这是几天了,现在我的工作Flutter代码通过以下教程,以学习如何使用Firebase与它。然而,我完全阻止了一个步骤,该人设法实现,但我复制了代码在100%,这里是我的错误:The method 'data' can't be unconditionally invoked because the receiver can be 'null'. Try making the call conditional (using '?.') or adding a null check to the target ('!').
下面是代码:
class GetUserData extends StatelessWidget {
final String documentId;
final String fieldName;
final String fieldTitle;
GetUserData(
{super.key,
required this.documentId,
required this.fieldName,
required this.fieldTitle});
@override
Widget build(BuildContext context) {
CollectionReference users = firestore.collection('Users');
return FutureBuilder(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
Map<String, dynamic> data = snapshot.data.data();
return ListTile(
title: Text(data[fieldName]),
subtitle: Text(data[fieldTitle]),
);
});
}
}
我试过很多方法:Map<String, dynamic> data = snapshot.data!.data();
return FutureBuilder( future: users.doc(documentId).get(), builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) { Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>; return ListTile( title: Text(data[fieldName]), subtitle: Text(data[fieldTitle]), ); });
在这里,当我使用_CastError (Null check operator used on a null value)
进行调试时,我没有看到错误,而是崩溃
我到处都找遍了,我不知道该怎么解决这个问题,
非常感谢你的帮助
2条答案
按热度按时间eqqqjvef1#
您没有正确解码来自firebase的响应,请尝试考虑以下代码:
yrdbyhpb2#
设法处理所有情况