flutter StreamBuilder快照始终返回空值和空安全错误

3hvapo4f  于 2023-01-31  发布在  Flutter
关注(0)|答案(1)|浏览(137)

这是我实现的代码,以获取可以在WhatsApp主页上看到的数据,即个人资料照片,联系人的姓名,最后一条消息,以及发送个人/组聊天列表的时间。

return firestore
        .collection('users')
        .doc(auth.currentUser!.uid)
        .collection('chats')
        .snapshots()
        .asyncMap((event) async {
      List<ChatContact> contacts = [];
      for (var document in event.docs) {
        var chatContact = ChatContact.fromMap(document.data());

        var userData = await firestore
            .collection('users')
            .doc(chatContact.contactID)
            .get();

        var user = model.User.fromMap(userData.data()!);

        contacts.add(
          ChatContact(
            name: user.name,
            profilePic: user.photoURL,
            contactID: chatContact.contactID,
            timeSent: chatContact.timeSent,
            lastMessage: chatContact.lastMessage,
          ),
        );
      }

      return contacts;
    });

我在StreamBuilder中调用此函数,以通过以下方式获取联系人列表:

StreamBuilder<List<ContactList>>(
              stream: ref.watch(chatControllerProvider).getchatContacts(),
              builder: (context, snapshot) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const CircularProgressIndicator();
                }
                return ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.data!.length,
                    itemBuilder: (context, index) {
                      var chatContactData = snapshot.data![index];
                      return Column(
                        children: [
                          InkWell(
                              onTap: () {
                                Navigator.pushNamed(
                                    context, UserChatScreen.routeName,
                                    arguments: {
                                      'name': 'rr',
                                      'selectedContactUID': 'uid'
                                    });
                              },
                              child: const ListTile(
                                title: Text(
                                  'name',
                                  style: const TextStyle(
                                    fontSize: 18,
                                  ),
                                ),
                                subtitle: Padding(
                                  padding: const EdgeInsets.only(top: 6.0),
                                  child: Text(
                                    ' lastMessage',
                                    style: const TextStyle(fontSize: 15),
                                  ),
                                ),
                                leading: CircleAvatar(
                                  backgroundImage: NetworkImage(
                                    'https://png.pngitem.com/pimgs/s/649-6490124_katie-notopoulos-katienotopoulos-i-write-about-tech-round.png',
                                  ),
                                  radius: 30,
                                ),
                                trailing: Text(
                                  'Date',
                                  style: const TextStyle(
                                    color: Colors.grey,
                                    fontSize: 13,
                                  ),
                                ),
                              ))
                        ],
                      );
                    });
              }),

这也是我现在面临的错误:

对空值使用了空检查运算符导致错误的相关小部件是StreamBuilder'

goqiplq2

goqiplq21#

当快照仍然没有data时,您尝试返回ListViewdata
将整个ListView.builder Package 在if(snapshot.hasData)

if(snapshot.hasData){
 return ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.data!.length,
                    itemBuilder: (context, index) {
                      var chatContactData = snapshot.data![index];
                      return Column(
                        children: [
                          InkWell(
                              onTap: () {
                                Navigator.pushNamed(
                                    context, UserChatScreen.routeName,
                                    arguments: {
                                      'name': 'rr',
                                      'selectedContactUID': 'uid'
                                    });
                              },
                              child: const ListTile(
                                title: Text(
                                  'name',
                                  style: const TextStyle(
                                    fontSize: 18,
                                  ),
                                ),
                                subtitle: Padding(
                                  padding: const EdgeInsets.only(top: 6.0),
                                  child: Text(
                                    ' lastMessage',
                                    style: const TextStyle(fontSize: 15),
                                  ),
                                ),
                                leading: CircleAvatar(
                                  backgroundImage: NetworkImage(
                                    'https://png.pngitem.com/pimgs/s/649-6490124_katie-notopoulos-katienotopoulos-i-write-about-tech-round.png',
                                  ),
                                  radius: 30,
                                ),
                                trailing: Text(
                                  'Date',
                                  style: const TextStyle(
                                    color: Colors.grey,
                                    fontSize: 13,
                                  ),
                                ),
                              ))
                        ],
                      );
                    });
}
return CircularProgressIndicator(); // 👈 Add this line to return something when if condition is not met.

相关问题