Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist, Firebase Flutter

Viewed 5310

i am getting data by document id but i get this error:

Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

and it's working, i can get the data from firebase by document id but it's giving the error in debug console.

I'm getting data with StreamBuilder:

 StreamBuilder(
          stream: _databaseService.productCollection.doc(docID).snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return Center(
                child: CircularProgressIndicator(
                  valueColor:
                      new AlwaysStoppedAnimation<Color>(Colorsx.mainColor),
                ),
              );
            }
            var document1 = snapshot.data;

            return Container(
              decoration: BoxDecoration(
                color: Colorsx.mainColor,
                borderRadius: radius,
              ),
              // color: Colorsx.mainColor,
              child: Column(
                children: [
                  Expanded(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Chip(
                          label: LabelText1("Ürün Adı:  "),
                          backgroundColor: Colorsx.mainColor,
                        ),
                        Chip(
                          shadowColor: Colorsx.mainColor2,
                          elevation: 24,
                          label: LabelText1(document1["productName"] ?? ""),
                          backgroundColor: Colorsx.mainColor2,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            );
          },
        ),

i couldn't find what is the problem in here but according to my researches, it' related with the map . is there any idea?

2 Answers

It looks like _databaseService.productCollection.doc(docID) may not point to an existing document at some point while this code runs. If you then call document1["productName"] on it as you do, it'll raise the error you see.

So you need to decide what to render when this situation happens (even if only briefly). For example, you could just make the CircularProgressIndicator stay on the screen until a document is available:

if (!snapshot.hasData || !snapshot.data.exists) {

if you're using cloud_functions ^2 and were using 1 you might be running into this issue, only way to deal with the error is with a try catch function

getSnapshot(DocumentSnapshot snapshot, String key) {
 try {
  return snapshot.get(key);
  } catch (error) {
   return null;
 }
}

then just call it

Status.fromSnapShot(DocumentSnapshot snapshot)
  : assert(snapshot.exists != false),
    name = getSnapshot(snapshot, 'name'),

before it was looking like this

Status.fromSnapShot(DocumentSnapshot snapshot)
  : assert(snapshot.exists != false),
    name = snapshot.data()['name'],
Related