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

Soner Karaevli

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?

Frank van Puffelen

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) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

How can I resolve " Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist"

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

Flutter: Unhandled Exception: Bad state: field does not exist within the DocumentSnapshotPlatform

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform) on flutter

flutter Bad state: field does not exist within the DocumentSnapshotPlatform

Flutter: Bad state: field does not exist within the DocumentSnapshotPlatform in streambuilder

Flutter - "Bad state: field does not exist within the DocumentSnapShotPlatform"

Problem with stream and firebase Bad state: field does not exist within the DocumentSnapshotPlatform

How to fix Bad state: field does not exist within the DocumentSnapshotPlatform Firebase Flutter FutureBuilder

I tried to display 'DateTime' on flutter UI get from firestore but show " Bad state: field does not exist within the DocumentSnapshotPlatform "

Bad state: field does not exist within the DocumentSnapshotPlatform flutter error even though field exists

Error: Bad state : field does not exist within the DocumentSnapshotPlatform

Bad state: field does not exist within the DocumentSnapshotPlatform error

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform). How can I get a doc from firestore?

Bad state: field does not exist within the DocumentSnapshotPlatform. Giving this error but don't know where the error is

field dos not exist within the DocumentSnapshotPlatform

Django: Get objects for which ForeignKey does not exist

The association refers to the inverse side field which does not exist

Flutter Firebase - Get a specific field from document

ant fileset cannot find directory which does exist

Using Django/Postgres get a calculated field which get a field value if exist if not another field value

Flutter Firebase - Snapshot cannot get data,

How to add "this user/account does not exist" error in Flutter, using Firebase?

Flutter bloc bad state of stream

Field does not exist ODOO

How to get all result if unwind field does not exist in mongodb

Bad state: You cannot close the subject while items are being added from addStream in flutter

flutter: Unhandled Exception: Bad state: Cannot add new events after calling close