firebase DART中的FutureBuilder:“无法无条件调用方法”data“,因为接收方可以为”null“,请尝试进行调用...”

rryofs0p  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(147)

这是几天了,现在我的工作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)进行调试时,我没有看到错误,而是崩溃
我到处都找遍了,我不知道该怎么解决这个问题,
非常感谢你的帮助

eqqqjvef

eqqqjvef1#

您没有正确解码来自firebase的响应,请尝试考虑以下代码:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
          child: FutureBuilder<DocumentSnapshot<Map<String, dynamic>>>(
        future: FirebaseFirestore.instance
            .collection('users')
            .doc(documentId) 
            .get(),
        builder: (_, snapshot) {
          if (snapshot.hasError) return Text('Error = ${snapshot.error}');
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Text("Loading");
          }
          Map<String, dynamic> data = snapshot.data!.data()!;
          return Text(data['fieldName']); // give your valid data here
        },
      )),
    );
  }
yrdbyhpb

yrdbyhpb2#

设法处理所有情况

@override
  Widget build(BuildContext context) {
    CollectionReference users = firestore.collection('Users');
    return FutureBuilder(
        future: users.doc(documentId).get(),
        builder:
            (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
           /// success fetch data
          if(snapshot.hasData){
           Map<String, dynamic> data = jsonDecode(snapshot.data);
           return ListTile(
            title: Text(data[fieldName]),
            subtitle: Text(data[fieldTitle]),
          );
          }
         /// failed and throw error
          else if(snapshot.hasError){
            return Text('${snapshot.error}');
          } 
        /// loading condition
        else {
        return Center(child:CircularProggressIndicator());
         }
        });
  }

相关问题