skip to Main Content
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.

2

Answers


  1. 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 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 the response to return and 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.

    I hope that helps!

    Login or Signup to reply.
  2. 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"];
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search