Streambuilder 快照为空且有错误

时间:2021-07-23 13:53:11

标签: flutter google-cloud-firestore snapshot stream-builder

我在桌面上重新安装了 flutter,更新了所有包和 flutter 版本(当前运行的是 2.2.3)。出于某种原因,我的 StreamBuilder 不再有效。我收到以下错误:“NoSuchMethodError:在 null 上调用了 getter 'isEmpty'。”

这是我的小部件的代码:

Container(
                        color: Theme.of(context).scaffoldBackgroundColor,
                        child: Column(
                          children: <Widget>[
                            StreamBuilder<List<User>>(
                                initialData: [],
                                stream:
                                    DatabaseService().getChattingWith(user.uid),
                                builder: (context, snapshot) {
                                  switch (snapshot.connectionState) {
                                    case ConnectionState.waiting:
                                      return Loading();
                                    default:
                                      List<User> userList = snapshot.data;
                                      if (snapshot.hasError) {
                                        print("something went wrong");
                                      }
if (!snapshot.hasData)return Text("No data");
                                      print(userList);

                                      return userList.isEmpty
                                          ? Center(
                                              child: Container(
                                                padding: EdgeInsets.fromLTRB(
                                                    5, 100, 5, 10),
                                                child: Column(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    Text("No Chats",
                                                        style: Theme.of(context)
                                                            .textTheme
                                                            .headline4),
                                                    SizedBox(
                                                      height: 30,
                                                    ),
                                                    Container(
                                                        width: 180.0,
                                                        height: 180.0,
                                                        decoration:
                                                            new BoxDecoration(
                                                          shape:
                                                              BoxShape.circle,
                                                          border: Border.all(
                                                            color: Theme.of(
                                                                    context)
                                                                .accentColor,
                                                            width: 4,
                                                          ),
                                                          image:
                                                              new DecorationImage(
                                                            image: new ExactAssetImage(
                                                                'assets/splashscreen.png'),
                                                            fit: BoxFit.cover,
                                                          ),
                                                        )),
                                                    SizedBox(
                                                      height: 40,
                                                    ),
                                                    Align(
                                                      alignment:
                                                          Alignment.center,
                                                      child: ArrowElement(
                                                        id: 'arrow',
                                                        show: true,
                                                        targetId: "profile",
                                                        tipLength: 20,
                                                        width: 5,
                                                        bow: 0.31,
                                                        padStart: 30,
                                                        padEnd: 43,
                                                        arcDirection:
                                                            ArcDirection.Right,
                                                        sourceAnchor:
                                                            Alignment.center,
                                                        targetAnchor:
                                                            Alignment(0, 0),
                                                        color: Theme.of(context)
                                                            .accentColor,
                                                        child: Column(
                                                          children: [
                                                            Text(
                                                                "Search to find matches",
                                                                style: Theme.of(
                                                                        context)
                                                                    .textTheme
                                                                    .headline5),
                                                          ],
                                                        ),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            )
                                          : ListView.builder(
                                              physics: BouncingScrollPhysics(),
                                              itemCount: userList.length,
                                              itemBuilder: (context, index) {
                                                return UserTile(
                                                  currentUser: user,
                                                  user: userList[index],
                                                );
                                              },
                                            );
                                  }
                                }),
                          ],
                        ),
                      ),

当运行代码时,“出现问题”被执行并且打印 userList 结果为空。流的代码是:

//Userlist from snapshot
  List<User> _userListFromSnapshot(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc) {
      return User(
          uid: doc['uid'],
          radius: doc['radius'] ?? 1,
          searching: doc['searching'] ?? false,
          searchTime: doc['searchTime'],
          timestamp: doc['timestamp'],
          displayName: doc['displayName'],
          email: doc['email']);
    }).toList();
  }
  //Get chattingWith stream
  Stream<List<User>> getChattingWith(String uid) {
    return FirebaseFirestore.instance
        .collection('users/$uid/chattingWith')
        .snapshots()
        .map(_userListFromSnapshot);
  }

chattingWith 集合只包含 documentID,isSeen 是一个布尔值,而 matchTime 是一个时间戳。但是之前用_userListFromSnapshot映射快照没有问题。

我只是不明白为什么会发生这种情况。非常感谢任何输入。

1 个答案:

答案 0 :(得分:1)

流可以处于更多状态,但您只处理 ConnectionState.waiting 和“其他一切”。

如果你保持你的代码不变,你应该在访问它的数据之前检查快照是否有数据,假设它存在:

if (!snapshot.hasData) return Text("No data");
return userList.isEmpty
  ...