Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

flutter - The method was called on null With DocumentSnapshot and Firestore

Basically I need to save in a list the fields of a collection in a firestore database.

This is the widget that takes care of connecting to the database:

Future<DocumentSnapshot> MissionRetriever1() async{
    return await FirebaseFirestore.instance
        .collection("quest")
        .doc("1")
        .get();
    }

And this is the widget contained in another file that takes care of saving the various data in a List :

 Future<void> getMission1() async{
    DocumentSnapshot snap = await widget.mservice.MissionRetriever1();
      Map<String,dynamic> data = snap.data();
      setState(() {
      MissionData1.add(data['Q_Category']);
      MissionData1.add(data['Q_Name']);
      MissionData1.add(data['Q_Description']);
      MissionData1.add(data['Q_Score']);
      MissionData1.add(data['Q_Target']);
    });
  }

But when I try to display a value inside the List in a Text widget like this:

Text(MissionData1.isEmpty ?  "Loading..." : MissionData1[1],
                                  style: TextStyle(
                                      fontWeight: FontWeight.bold,
                                      fontFamily: 'Roboto',
                                      color: const Color(0xFF3A404C),
                                      fontSize: 14.0.sp),),

It always stays in : "Loading..." and return this error :

E/flutter ( 3639): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'MissionRetriever1' was called on null.
E/flutter ( 3639): Receiver: null
E/flutter ( 3639): Tried calling: MissionRetriever1()

This is the Firestore Collection :

Firestore

This is the init method :

@override
  void initState() {
    super.initState();
    asyncMethod();
  }

  void asyncMethod() async {
    **await getMission1();**
    await widget.auth.retrieveScore();
    **await widget.mservice.MissionRetriever1();**
    await widget.mservice.NumberGenerator();
  }

And this is the List initialization : **List<String> MissionData1 = [];**

P.S : Looking at the Firestore logs no connection was refused

question from:https://stackoverflow.com/questions/65846954/the-method-was-called-on-null-with-documentsnapshot-and-firestore

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change your method "MissionRetriever1()" and see if you are able to recover the data

return FirebaseFirestore firestore = FirebaseFirestore.instance
.collection('quest')
.doc("1")
.get()
.then((DocumentSnapshot documentSnapshot) {
  if (documentSnapshot.exists) {
    print('Document data: ${documentSnapshot.data()}');
  } else {
    print('Document does not exist on the database');
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...