Search code examples
flutterfutureflutter-futurebuilder

LateInitializationError: Field has not been initialized.' .then() command not running after running firebase query in FutureBuilder


I am trying to retrieve a Firestore Snapshot and my code doesn't seem to be working. I made sure fireUser.uid was working, and it printed the right ID but strangely my .then() code isn't running at all I put print('then') in it and isn't appearing on my console

this is where the error is occurring:

FutureBuilder(
      future: Future.wait([
      DatabaseService.getUserDataFromFirestore(FirebaseAuth.instance.currentUser!),
      GeoService.getPosition(),
      ]),
      builder: (context, snap) {
        if (snap.connectionState == ConnectionState.done) {
          return Frame();
        }
        else return Container(
          color: Colors.black,
          child: Center(
            child: spinKit,
          ),
        );
      }
    );

Future with error:

static Future<Userdata> getUserDataFromFirestore (User fireUser) async {
    await usersRef.doc(fireUser.uid).get().then((val) {
      print('then');
      userdata = Userdata.fromDoc(val);
    });
    return userdata;
  }

error message:

LateInitializationError: Field 'userdata' has not been initialized.

Solution

  • I had a different problem then I thought. Firestore must have been updated because the rules of my Firestore database kept me locked out so I updated the rules and now my code works fine. Thanks so much for the help