gpt4 book ai didi

flutter - 信息:此函数的返回类型为 'Widget',但不以return语句结尾

转载 作者:行者123 更新时间:2023-12-03 04:11:20 24 4
gpt4 key购买 nike

我正在尝试找出为什么在Streambuilder返回时出现此错误的原因

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
import 'package:google_fonts/google_fonts.dart';

import 'package:paylaterapp/screens/MerchantDetails.dart';

class MerchantList extends StatefulWidget {
@override
_MerchantListState createState() => new _MerchantListState();
}

class _MerchantListState extends State<MerchantList> {
StreamController _postsController;
final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();

int count = 1;

Future fetchPost([howMany = 10]) async {
final response = await http.get('http://localhost/categories/fitness/$howMany');

if (response.statusCode == 200) {
return json.decode(response.body);
} else {
throw Exception('Failed to load merchant');
}
}

loadPosts() async {
fetchPost().then((res) async {
_postsController.add(res);
return res;
});
}

showSnack() {
return scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text('New content loaded'),
),
);
}

Future<Null> _handleRefresh() async {
count++;
print(count);
fetchPost(count).then((res) async {
_postsController.add(res);
showSnack();
return null;
});
}

@override
void initState() {
_postsController = new StreamController();
loadPosts();
super.initState();
}

@override
Widget build(BuildContext context) {
return new Scaffold(
key: scaffoldKey,
appBar: new AppBar(
title: Text('Explore Retailers',
style: GoogleFonts.rubik(
fontWeight: FontWeight.w700
)
),
actions: <Widget>[
IconButton(
tooltip: 'Refresh',
icon: Icon(Icons.refresh),
onPressed: _handleRefresh,
)
],
),
body: StreamBuilder(
stream: _postsController.stream,
builder: (BuildContext context, AsyncSnapshot snapshot) {
print('Has error: ${snapshot.hasError}');
print('Has data: ${snapshot.hasData}');
print('Snapshot Data ${snapshot.data}');

if (snapshot.hasError) {
return Text(snapshot.error);
}

if (snapshot.hasData) {
return Column(
children: <Widget>[
Expanded(
child: Scrollbar(
child: RefreshIndicator(
onRefresh: _handleRefresh,
child: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
var post = snapshot.data[index];
return GestureDetector(
onTap: () {
print(post);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
GridMerchantDetails(post)),
);
},
child: Container(
height: 160,
margin: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
decoration: BoxDecoration(
color: Colors.grey,
image: DecorationImage(
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(
Colors.black.withOpacity(0.5),
BlendMode.darken),
image: new NetworkImage(
post['assets']['backgroundimage_url'] != null
? 'https:' +
post['assets']['backgroundimage_url']
: 'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
))),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FadeInImage.assetNetwork(
image: post['assets']['logo_url'] != null
? 'https:' + post['assets']['logo_url']
: 'https://images.unsplash.com/photo-1446844805183-9f5af45f89ee',
placeholder: 'assets/images/transparent.png',
width: 140,
height: 140,
fit: BoxFit.contain
)
]
)
)
);
},
),
),
),
),
],
);
}

if (!snapshot.hasData &&
snapshot.connectionState != ConnectionState.done) {
return Text('No Merchants');
}

if (snapshot.connectionState != ConnectionState.done) {
return Center(
child: CircularProgressIndicator(),
);
}


},
),
);
}
}

最佳答案

如果您在StreamBuilder的末尾添加一个后备builder,以防其他所有失败,则应解决此问题:

...
if (snapshot.connectionState != ConnectionState.done) {
return Center(
child: CircularProgressIndicator(),
);
}

// This line at the very end after the last `if` statement
return Center(child: Text('Data unavailable'));

您还应该确保考虑到所有可能的失败。与连接和可能的 return数据

关于flutter - 信息:此函数的返回类型为 'Widget',但不以return语句结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59784504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com