我在数据库(firebase firestore)中进行搜索,并且我希望屏幕表现如下:搜索运行时,显示一个CircularProgressIndicator。搜索完成后,如果找到数据,请显示数据。否则,显示“找不到记录”消息。我的问题是,CircularProgressIndicator显示处于永恒循环中。我该怎么办?
Stream<QuerySnapshot> getStreamOfBooks() {
return bookCollection.snapshots();
}
j
body: StreamBuilder<QuerySnapshot>(
stream: repository.getStreamOfBooks(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData) {
return _buildList(context, snapshot.data.documents);
} else {
return Text("Records not found");
}
}
else {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
child: new Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new CircularProgressIndicator(),
new Text("Loading")
]));
}
})
将其更改为以下内容:
body: StreamBuilder<QuerySnapshot>(
stream: repository.getStreamOfBooks(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return _buildList(context, snapshot.data.documents);
}
else if (snapshot.connectionState == ConnectionState.waiting) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0))),
child: new Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
new CircularProgressIndicator(),
new Text("Loading")
]));
}
else if (snapshot.connectionState == ConnectionState.none) {
return Text("No data");
}
})
[ConnectionState.done
表示它返回了数据,ConnectionState.waiting
表示它仍在等待异步操作完成,ConnectionState.none
表示它没有返回任何数据。