0

I can't seem to pass a QuerySnapshot from one class to another without losing the Snapshot so I am trying to find a work around (see my post here). I am trying to pull the data again by using a StreamBuilder.

StreamBuilder (
        stream: _db.collection('agency').doc(globals.agencyId).
        collection('trxns').doc(globals.currentTrxnId).snapshots(),
        builder: (context, trxnSnapshot) {  <<<< ERROR 1
        if (trxnSnapshot.hasData) {
          var outPut = trxnSnapshot.data();  <<<< ERROR 2
          clientFNameController.text = trxnSnapshot.data.data['clientFName'] ?? ""; <<<< ERROR 3
        }
)

I am getting some errors.

  1. The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. I have tried adding "?" and "!" but it is not working.

  2. Found this line of code in a post but I am not sure how to get the data from the snapshot.

  3. This error will be fixed after #2 is fixed.

These should be easy fixes but I'm not sure how to do it.

EDIT I tried adding type casting as shown below but I get new errors.

StreamBuilder<QuerySnapshot> (
        stream: _db.collection('agency').doc(globals.agencyId).
        collection('trxns').doc(globals.currentTrxnId).snapshots(),
        builder: (context, trxnSnapshot) {
          if (trxnSnapshot.hasData) {
            var outPut = (trxnSnapshot.data() as QueryDocumentSnapshot);
                        clientFNameController.text = trxnSnapshot.data.data['clientFName'] ?? "";
          }
)

enter image description here

LostTexan
  • 431
  • 5
  • 18

1 Answers1

0

You should first check if the snapshot you are receiving has any data. If not, it is impossible to call trxnSnapshot.data()

Try this:

StreamBuilder (
        stream: _db.collection('agency').doc(globals.agencyId).
        collection('trxns').doc(globals.currentTrxnId).snapshots(),
        builder: (context, trxnSnapshot) {
          if(trxnSnapshot.hasData){  
            var outPut = trxnSnapshot.data();  
            clientFNameController.text = trxnSnapshot.data.data['clientFName'] ?? "";
       } else {
            print("Do something, maybe show a loading widget");
    }
          
)
Alan Cesar
  • 370
  • 3
  • 13
  • Thanks @AlanCesar for you response but I still have the errors. They are connected with null safety. Do you have any suggestions on how to fix this? – LostTexan Jul 28 '21 at 13:31