Flutter-CircularProgressIndicator始终运行

问题描述 投票:0回答:1

我在数据库(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")
                      ]));
            }
          })
android ios firebase flutter dart
1个回答
0
投票

将其更改为以下内容:

 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表示它没有返回任何数据。

© www.soinside.com 2019 - 2024. All rights reserved.