这是我实现的代码,以获取可以在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'
1条答案
按热度按时间goqiplq21#
当快照仍然没有
data
时,您尝试返回ListView
和data
。将整个
ListView.builder
Package 在if(snapshot.hasData)
内