Omar Hajj
Omar Hajj

Reputation: 41

type 'Future<dynamic> is not subtype of type 'String'

So i am trying to retrieve data from my firestore document which is an email I am not understanding how i can save the data as a string when i retrieve it from firestore. help pls.

Getting error: type 'Future is not subtype of type 'String' on result: getEmail() Ps: im new to flutter my code:

getEmail() async{
  String _email = (await FirebaseAuth.instance.currentUser()).email;
  return _firestore.collection('users').document(_email).collection('met_with').document('email').get();
}

...

children: <Widget>[
  BottomSheetText(question: 'Email', result:getEmail()),
  SizedBox(height: 5.0),
....

Upvotes: 0

Views: 240

Answers (1)

dumazy
dumazy

Reputation: 14445

..document('email)'.get() will actually return Future<DocumentSnapshot>. So getEmail() doesn't return a String.

You need to get the data out of the DocumentSnapshot:

Future<String> getEmail() async {
  String _email = (await FirebaseAuth.instance.currentUser()).email;
  DocumentSnapshot snapshot = await _firestore.collection('users')
    .document(_email)
    .collection('met_with')
    .document('email')
    .get();
  // print("data: ${snapshot.data}"); // might be useful to check
  return snapshot.data['email']; // or another key, depending on how it's saved
}

For more info, check out the documentation in the API reference

Now, besides that, getEmail() is an async function. Calling it from your widget tree is probably not how you want to handle this. You'll need to wait for the result of getEmail() before you can use it in your UI. A FutureBuilder might help:

children: <Widget>[
  FutureBuilder<String>(
    future: getEmail(),
    builder: (context, snapshot) {
      if(!snapshot.hasData) {
        return CircularProgressIndicator(); // or null, or any other widget
      }
      return BottomSheetText(question: 'Email', result: snapshot.data);
    },
  ),
  SizedBox(height: 5.0),
  ... //other
],

Upvotes: 0

Related Questions