Home > Mobile >  Type 'Future<QuerySnapshot<Map<String, dynamic>>>' is not a subtype of t
Type 'Future<QuerySnapshot<Map<String, dynamic>>>' is not a subtype of t

Time:01-04

static CollectionReference doses =
      FirebaseFirestore.instance.collection('Doses');
  void setDoseDetails(
      TextEditingController endController,
      TextEditingController startController,
      TextEditingController doseController,
      int noPills,
      int doseRep) {
    var dose =
        doses.where('userID', isEqualTo: Auth().uID).get() as DocumentSnapshot;
    Map<String, dynamic> userData = dose as Map<String, dynamic>;
    endController.text = userData['endDate'];
    startController.text = userData['startDate'];
    noPills = userData['noPills'];
    doseController.text = userData['doseVal'];
    doseRep = userData["doseRep"];
  }

I'm trying to retrieve data from Firebase using this code and it's not working.

CodePudding user response:

If you see the return type of the get() method that you are trying to call in doses.where('userID', isEqualTo: Auth().uID).get() it is Future<QuerySnapshot<T>>. There are two problems in your approach:

  1. You are not awaiting the for the result of doses.where('userID', isEqualTo: Auth().uID).get().

  2. You are forcefully type-casting a QuerySnapshot into a DocumentSnapshot. If doses.where('userID', isEqualTo: Auth().uID).get() after awaiting, returns a QuerySnapshot, the variable holding that value should also be of the type QuerySnapshot.

Here is what you can do instead:

  static CollectionReference doses =
      FirebaseFirestore.instance.collection('Doses');
  Future<void> setDoseDetails(
      TextEditingController endController,
      TextEditingController startController,
      TextEditingController doseController,
      int noPills,
      int doseRep) async {
    QuerySnapshot dose =
        await doses.where('userID', isEqualTo: Auth().uID).get();
    Map<String, dynamic> userData = dose as Map<String, dynamic>;
    endController.text = userData['endDate'];
    startController.text = userData['startDate'];
    noPills = userData['noPills'];
    doseController.text = userData['doseVal'];
    doseRep = userData["doseRep"];
  }

If you notice in the solution above,

  1. I have changed the return type of setDoseDetails because it can't be just void and has to be Future<void> because you are dealing with futures.

  2. I have put an await keyword in front of the doses.where('userID', isEqualTo: Auth().uID).get(). This will allow for the response to return and then be assigned to the variable dose.

To read more about futures I would suggest going through https://api.flutter.dev/flutter/dart-async/Future-class.html.

Hope that helps!

CodePudding user response:

This might work for you:

var dose =
        await doses.where('userID', isEqualTo: Auth().uID).get();
    var userData = dose.docs.first.data();
    endController.text = userData['endDate'];
    startController.text = userData['startDate'];
    noPills = userData['noPills'];
    doseController.text = userData['doseVal'];
    doseRep = userData["doseRep"];
  • Related