我从Firestore返回的数据有问题。我需要使用Cloud Firestore Plugin来获得一些电子邮件:
class _EmailPageState extends State<EmailPage> {
//StreamBuilder<QuerySnapshot> _emails;
@override
void initState() {
super.initState();
//_emails = _getEmails();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Refer user'),
),
body: Column(
children: <Widget>[
Form(... form code)
Expanded(child: _getEmails()),
],
)
}
由于直到数据从Firestore返回之前不会填充_emails,所以我得到:
flutter: The following assertion was thrown building Expanded(flex: 1, dirty):
flutter: A build function returned null.
flutter: The offending widget is: Expanded(flex: 1)
flutter: Build functions must never return null. To return an empty space that causes the building widget to
flutter: fill available room, return "new Container()". To return an empty space that takes as little room as
flutter: possible, return "new Container(width: 0.0, height: 0.0)".
我不知道如何解决该错误。视图显示正确。但是我不想出错。
StreamBuilder<QuerySnapshot> _getEmails() {
final query = Firestore.instance
.collection('emails')
.where("referer", isEqualTo: uid)
.snapshots();
return StreamBuilder<QuerySnapshot>(
stream: query,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) return Text('Error: ${snapshot.error}');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Text('Loading...');
default:
return _makeList(snapshot);
}
},
);
//setState(() {
//_emails = emailList;
//});
//return emailList;
}
ListView _makeList(AsyncSnapshot<QuerySnapshot> snapshot) {
return ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return ListTile(
title: Text(document['email']),
);
}).toList(),
);
}
答案 0 :(得分:0)
您的_getEmails()
在数据到达之前返回null。它应该返回类似CircularProgressIndicator()
的内容。如果显示该功能,则可以获得更多帮助,但是您也可以从此处使用示例代码:https://docs.flutter.io/flutter/widgets/StreamBuilder-class.html
编辑:好的,我看到您的编辑。我猜它毕竟不会返回null。但是,Expanded
必须是Row
或Column
的子代。您应该使用其中一种将其包装起来,或者使用Container
,Center
等进行定位。
此外,无需缓存StreamBuilder
,因此不应该缓存小部件。但是,您可以缓存流。您至少应该将代码更改为
class _EmailPageState extends State<EmailPage> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Refer user'),
),
body: Center(child: _getEmails())
)
}
}