Android 在Flatter中使用stream builder时,我不断遇到一个轻微的空错误
在显示我的项目之前,我一直收到一个轻微的空错误。请告诉我如何以最佳方式使用streambuilder而不会遇到任何错误,好吗?Android 在Flatter中使用stream builder时,我不断遇到一个轻微的空错误,android,ios,firebase,flutter,google-cloud-firestore,Android,Ios,Firebase,Flutter,Google Cloud Firestore,在显示我的项目之前,我一直收到一个轻微的空错误。请告诉我如何以最佳方式使用streambuilder而不会遇到任何错误,好吗?streambuilder( StreamBuilder<QuerySnapshot>( stream: Firestore.instance.collection('users').snapshots(), builder: (context, snapshot) { if (snapshot.hasError) return Cente
streambuilder(
StreamBuilder<QuerySnapshot>(
stream:
Firestore.instance.collection('users').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError)
return Center(child:Text('${snapshot.error.toString()}'));
if (snapshot.hasData) {
final List<DocumentSnapshot> documents = snapshot
.data.documents;
return ListView.builder(
itemCount: documents.length,
itemBuilder: (context, index) => RestaurantCard(
documents[index].documentID,
name: documents[index].data['name'],
address: documents[index].data['address'],
image: documents[index].data['photo'],
),
scrollDirection: Axis.horizontal,
);
} else {
return Center(child: CircularProgressIndicator());
}
},
流:
Firestore.instance.collection('users').snapshots(),
生成器:(上下文,快照){
if(snapshot.hasError)
返回中心(子项:文本('${snapshot.error.toString()}');
if(snapshot.hasData){
最终列表文档=快照
.数据、文件;
返回ListView.builder(
itemCount:documents.length,
itemBuilder:(上下文,索引)=>RestaurantCard(
文档[index].documentID,
名称:文档[索引]。数据['name'],
地址:文档[索引]。数据['address'],
图像:文档[index]。数据['photo'],
),
滚动方向:轴水平,
);
}否则{
返回中心(子项:CircularProgressIndicator());
}
},
)使用
连接状态
:
StreamBuilder(
stream: Firestore.instance.collection('stores').document(currentUserUID).snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return ListView.builder(
shrinkWrap: true,
itemCount: 1,
itemBuilder: (BuildContext context, int index) {
return ListTile(
contentPadding: EdgeInsets.all(8.0),
title: Text(snapshot.data.data["about"]),
leading: Text(
snapshot.data.data["location"],
));
});
} else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
return CircularProgressIndicator();
},
),
StreamBuilder(
流:Firestore.instance.collection('stores').document(currentUserUID).snapshots(),
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
返回ListView.builder(
收缩膜:对,
物品计数:1,
itemBuilder:(构建上下文,int索引){
返回列表块(
contentPadding:EdgeInsets.all(8.0),
标题:文本(snapshot.data.data[“about”]),
前导:文本(
snapshot.data.data[“位置”],
));
});
}else if(snapshot.connectionState==connectionState.none){
返回文本(“无数据”);
}
返回循环ProgressIndicator();
},
),
您能否共享一段代码片段,以便so的人员可以查看并帮助您解决问题?在使用StreamBuilder时,您不能放置ConnectionState,因为流始终处于活动状态。您必须使用snapshot.hasData。您可以将ConnectionState与FutureBuilder一起使用—没有问题