Flutter & Firebase:无法将数据从 firebase 子集合获取到我的 Stream
Flutter & Firebase : Failed to get data from firebase sub-collection into my Stream
我正在尝试获取我的数据信息。出于某种原因,我做不到。它甚至没有打印出我的内部打印语句“打印(“内部快照”)”。
请查看我的代码并告诉我如何解决此问题。
我期待着提前听到您和 ideas.Thank 您的所有意见。
终端消息
W/DynamiteModule( 3822): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 3822): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 3822): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/flutter ( 3822): Inside _messageList
型号
class Conversation {
final DateTime date;
final String message;
final String sendby;
Conversation({this.date, this.message, this.sendby});
}
屏幕代码
return StreamBuilder<List<Conversation>>(
stream: User_DatabaseService(uid: uid).chatroomMesssages,
builder: (context, snapshot) {
List<Conversation> messageList = snapshot.data ?? [];
messageList.forEach((message) {
print("${message.date} ${message.message} ${message.sendby}");
});
//-----
}
数据库代码
// collection reference
final CollectionReference chatroomCollection =
FirebaseFirestore.instance.collection('chatrooms');
// all conversation
List<Conversation> _messageList(QuerySnapshot snapshot) {
print("Inside _messageList");
return snapshot.docs.map((doc) {
print("Inside snapshot");
final dataInfo = doc.data();
print(dataInfo['date']);
return Conversation(
date: dataInfo['date'] ?? null,
sendby: dataInfo['sendBy'] ?? null,
message: dataInfo['message'] ?? null,
);
}).toList();
}
//Get all conversation
Stream<List<Conversation>> get chatroomMesssages {
//print('in allUserData');
print("Inside getChatroomMesssages");
return userCollection
.doc('devil@yahoo.com_punreach@yahoo.com')
.collection("chats")
.snapshots()
.map(_messageList);
}
Firebase
请阅读错误信息,它实际上提供了很多信息:
type 'Conversation' is not a subtype of type 'List'
看看这一行;如果 snapshot.data
为 null,那么您正在尝试分配错误类型的对象:
List<Conversation> messageList = snapshot.data ?? Conversation();
编辑:请再次阅读实际的错误消息。
The method 'forEach' was called on null.
现在,如果 snapshot.data
为空,则将 null
赋值给同一个变量,此处:List<Conversation> messageList = snapshot.data ?? null;
,然后调用 .forEach
。
Firebase 中没有“此集合不存在”错误。您可以引用不存在的集合,但它们是空的。您可以引用不存在的集合,这就是您首先创建它们的方式。
这里的错误是您使用的 userCollection
没有这些。在您的屏幕截图中我们看到 chatrooms
,因此您应该改用 chatroomsCollection
,它实际上具有我们在屏幕截图中看到的数据。
我正在尝试获取我的数据信息。出于某种原因,我做不到。它甚至没有打印出我的内部打印语句“打印(“内部快照”)”。
请查看我的代码并告诉我如何解决此问题。 我期待着提前听到您和 ideas.Thank 您的所有意见。
终端消息
W/DynamiteModule( 3822): Local module descriptor class for providerinstaller not found.
I/DynamiteModule( 3822): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller( 3822): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/flutter ( 3822): Inside _messageList
型号
class Conversation {
final DateTime date;
final String message;
final String sendby;
Conversation({this.date, this.message, this.sendby});
}
屏幕代码
return StreamBuilder<List<Conversation>>(
stream: User_DatabaseService(uid: uid).chatroomMesssages,
builder: (context, snapshot) {
List<Conversation> messageList = snapshot.data ?? [];
messageList.forEach((message) {
print("${message.date} ${message.message} ${message.sendby}");
});
//-----
}
数据库代码
// collection reference
final CollectionReference chatroomCollection =
FirebaseFirestore.instance.collection('chatrooms');
// all conversation
List<Conversation> _messageList(QuerySnapshot snapshot) {
print("Inside _messageList");
return snapshot.docs.map((doc) {
print("Inside snapshot");
final dataInfo = doc.data();
print(dataInfo['date']);
return Conversation(
date: dataInfo['date'] ?? null,
sendby: dataInfo['sendBy'] ?? null,
message: dataInfo['message'] ?? null,
);
}).toList();
}
//Get all conversation
Stream<List<Conversation>> get chatroomMesssages {
//print('in allUserData');
print("Inside getChatroomMesssages");
return userCollection
.doc('devil@yahoo.com_punreach@yahoo.com')
.collection("chats")
.snapshots()
.map(_messageList);
}
Firebase
请阅读错误信息,它实际上提供了很多信息:
type 'Conversation' is not a subtype of type 'List'
看看这一行;如果 snapshot.data
为 null,那么您正在尝试分配错误类型的对象:
List<Conversation> messageList = snapshot.data ?? Conversation();
编辑:请再次阅读实际的错误消息。
The method 'forEach' was called on null.
现在,如果 snapshot.data
为空,则将 null
赋值给同一个变量,此处:List<Conversation> messageList = snapshot.data ?? null;
,然后调用 .forEach
。
Firebase 中没有“此集合不存在”错误。您可以引用不存在的集合,但它们是空的。您可以引用不存在的集合,这就是您首先创建它们的方式。
这里的错误是您使用的 userCollection
没有这些。在您的屏幕截图中我们看到 chatrooms
,因此您应该改用 chatroomsCollection
,它实际上具有我们在屏幕截图中看到的数据。