0

I'm getting green swirly lines underneath the await keyword.

When I hover the mouse over the green swirly lines, it says 'await' applied to 'Query', which is not a 'Future'.

I'm not sure to which statement the await keyword needs to be applied.

Without applying the await/async and Future to the method the calling method doesn't wait for this method to get completed and continues with executing the rest of the code, therefore returning a null value.

Please find below the code which is in question:

Future<List<ContData>> readCC(String tID) async {
    List<ContData> conList = [];
    try {
      final conCollection = await FirebaseFirestore.instance
          .collection('CC')
          .where('conID', isEqualTo: tID);

      await conCollection.snapshots().listen((snapshot) {
        var tc = snapshot.docs[0].data()['con'];
        ContData con;
        for (var t in tc) {
          con = ContData.fromMap(data: t);
          print(con);
          final conType = con.conType;
          final conText = con.conText;
          final conCodeCaption = con.conCodeCaption;
          final conCodeText = con.conCodeText;
          final conCodeOutput = con.conCodeOutput;
          final conImageFilename = con.conImageFilename;
          final conImageCaption = con.conImageCaption;
          final conImageCredit = con.conImageCredit;
          final conImageCreditLink = con.conImageCreditLink;
          final conAnchorText = con.conAnchorText;
          final conAnchorLink = con.conAnchorLink;
          final ContData = ContData(
              conType: conType,
              conText: conText,
              conCodeCaption: conCodeCaption,
              conCodeText: conCodeText,
              conCodeOutput: conCodeOutput,
              conImageFilename: conImageFilename,
              conImageCaption: conImageCaption,
              conImageCredit: conImageCredit,
              conImageCreditLink: conImageCreditLink,
              conAnchorText: conAnchorText,
              conAnchorLink: conAnchorLink);
        }
      });
    } catch (e) {
      print(e.toString());
    }
    return conList;
  }

Thank you so much for your precious time and help in advance!

xpetta
  • 607
  • 12
  • 28

1 Answers1

1

You can only add await to a method that returns a Future, for example:

final conCollection = await FirebaseFirestore.instance
          .collection('CC')
          .where('conID', isEqualTo: tID).get();

The method get() returns a Future<QuerySnapshot> therefore instead of using then() which takes a callback that will be called when the future is completed.

Read the following guide:

https://dart.dev/codelabs/async-await

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thank you so much @Peter Haddad, TBH I was watching videos on your channel just 2 days back, thanks for the wonderful content as well :) – xpetta Nov 18 '20 at 20:29
  • I'm not sure if it was a YouTube channel or Udemy course. I have been hopping all around to learn Flutter. – xpetta Nov 18 '20 at 20:37
  • 1
    Hello @Peter Haddad, I recall now I read your articles on Medium https://medium.com/@peterhaddad – xpetta Nov 20 '20 at 11:57