damnexpress
damnexpress

Reputation: 61

I want to get the field data from FireStore

I want to get ['startTime] in this method.

But I can't get it.

I get the following error

The method 'data' isn't defined for the type 'Future'. Try correcting the name to the name of an existing method, or defining a method named 'data'.

  Future<String> getStudyTime()async {
    final getStartTime =
        await FirebaseFirestore.instance.collection('user').doc(uid()).get().data()['startTime'];
    final DateTime now = DateTime.now();
    final formatTime = DateFormat('hh:mm a').format(now);
    var hh = now.hour;
    var mm = now.minute;
    var hhmm = "$hh:$mm";
    studyTime = int.parse(hhmm);

    return studyTime;
  }

[FireStore Image] enter image description here

Upvotes: 0

Views: 41

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Calling get() returns a Future, so you need to use await on get to get its value:

final doc = await FirebaseFirestore.instance.collection('user').doc(uid()).get();

final getStartTime = doc.data()['startTime'];

If you want to do this in a single line, use parenthesis to ensure the await works on get():

final getStartTime =
  await (FirebaseFirestore.instance.collection('user').doc(uid()).get()).data()['startTime'];

Upvotes: 1

Related Questions