我正在尝试从appBar上的firebase检索用户名。它成功检索到它。但是在成功显示用户名之前,它会在屏幕上显示错误几秒钟。错误os
I / flutter(24143):构建时引发了以下NoSuchMethodError StreamBuilder(脏,状态:
I / flutter(24143):_StreamBuilderBaseState
#c10cf): I / flutter(24143):在null上调用了getter'documents'。
I / flutter(24143):接收方:空
I / flutter(24143):尝试调用:文档
Class Data{
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
CurrentUser _currentUser = Provider.of<CurrentUser>(context, listen: false);
return Scaffold(
backgroundColor: Colors.grey[600],
resizeToAvoidBottomPadding: false,
appBar: AppBar(
title: Text('Property Host'),
centerTitle: true,
actions: <Widget>[
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Container(
margin: new EdgeInsets.only(left: 50),
child: Text('Property Host',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 19),)),
StreamBuilder(stream: Firestore.instance.collection('users').where("uid", isEqualTo: userid).snapshots(),
// ignore: missing_return
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.data == null)
CircularProgressIndicator();
//final userDocument = snapshot.data;
//final title= snapshot.data.userocument['displayName']);
//CircularProgressIndicator();
return Expanded(
child: ListView.builder(
itemCount: snapshot.data.documents.length,
// ignore: missing_return
itemBuilder: (BuildContext context, int index) {
print(user.uid);
return user != null
? Container(
margin: EdgeInsets.only(top: 17, left: 40),
child: Text(
snapshot.data.documents.elementAt(index)['displayName']),
)
: IconButton(
icon: Icon(Icons.person),
// ignore: missing_return
onPressed: () {
Navigator.pushNamed(context, '/LoginScreen');
},
);
}
),
);
}
答案 0 :(得分:1)
使用snapshot.hasData来确保您在检索到数据后构建主窗口小部件(扩展),并使用CircularProgessIndicator在返回数据之前保持其位置
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData){
return Expanded(
child: ListView.builder(
itemCount: snapshot.data.documents.length,
// ignore: missing_return
itemBuilder: (BuildContext context, int index) {
print(user.uid);
return user != null
? Container(
margin: EdgeInsets.only(top: 17, left: 40),
child: Text(
snapshot.data.documents.elementAt(index)['displayName']),
)
: IconButton(
icon: Icon(Icons.person),
// ignore: missing_return
onPressed: () {
Navigator.pushNamed(context, '/LoginScreen');
},
);
}
),
);
}
else {
return CircularProgressIndicator();
}
}