如何解决尝试从 Stream Provider 检索和显示数据时出错
我在尝试从 Stream Provider 检索数据时遇到以下错误。
======== Exception caught by provider ==============================================================
The following assertion was thrown:
An exception was throw by _MapStream<DocumentSnapshot<Map<String,dynamic>>,UserData> listened by
StreamProvider<UserData>,but no `catchError` was provided.
Exception:
NoSuchMethodError: The method '[]' was called on null.
Receiver: null
Tried calling: []("firstName")
====================================================================================================
下面是与Stream相关的代码:
class DatabaseStream {
final CollectionReference userCollection = FirebaseFirestore.instance.collection('users');
String uid;
DatabaseStream({this.uid});
Stream<UserData> get userData {
return userCollection.doc(uid).snapshots().map((docSnapshot) => UserData.fromDocumentSnapshot(snapshot: docSnapshot));
}
}
下面的方法用于解析Stream数据:
UserData.fromDocumentSnapshot({DocumentSnapshot snapshot}) {
Map<String,dynamic> snapshotData = snapshot.data();
uid = snapshot.id;
firstName = snapshotData['firstName'];
lastName = snapshotData['lastName'];
email = snapshotData['emailAddress'];
accountCreated = snapshotData['accountCreated'];
}
以下是用于访问流的代码:
UserData _userInfo = Provider.of<UserData>(context);
print('--- From UserInfoStream ---');
print(_userInfo);
print(_userInfo.uid);
print(_userInfo.firstName);
print(_userInfo.email);
print(_userInfo.accountCreated);
我正在使用以下版本的软件和软件包 Flutter 2.0.3 • channel stable
、Dart 2.12.2
和 provider: ^4.3.2+1
。
如果需要任何其他详细信息,请告诉我,我非常乐意提供。
提前感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。