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

myke

I'm new to flutter. I want to get a specific doc from firestore using its uid and then transfer this doc in a value type that I can handle. BUT i get a breakpoint when I run my application : StateError (Bad state: field does not exist within the DocumentSnapshotPlatform). This the code:

final cloth = await DatabaseService(uid: uid)
          .clothCollection
          .doc(uid)
          .get()
          .then((doc) => {
                            Cloth(
                    brand: doc.get('brand'),
                    name: doc.get('name'),
                    color: doc.get('color'),
                    url: doc.get('url'))
              });

and Cloth :

class Cloth {
  final String? name;
  final String? url;
  final String? brand;
  final String? color;

  Cloth({this.name, this.brand, this.color, this.url});
}

If there is an easier way to get this doc let me know. Thanks for your help!

Unbreachable

The get attribute does not exist on the DocumentSnapshot(doc). Instead, you have to use the data() method which returns a Map<String, dynamic>, or null if the doc does not exist. It's good practice to always check to see if the document exists first before executing further.

final cloth = await DatabaseService(uid: uid)
          .clothCollection
          .doc(uid)
          .get()
          .then((doc) {
            if (doc.exists) {
            Map<String, dynamic>? data = doc.data();

              return Cloth(
                    brand: data?['brand'],
                    name: data?['name'],
                    color: data?['color'],
                    url: data?['url']);
           } else {
              print('Document does not exist.');
            }
              });

Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.

En caso de infracción, por favor [email protected] Eliminar

Editado en
0

Déjame decir algunas palabras

0Comentarios
Iniciar sesiónRevisión de participación posterior

Artículos relacionados

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

How to get the modified field or data from the doc Firebase Firestore realtime updates?

How can I get the deployment name from within my container?

Can I update a parent state from within a Dialog in Flutter?

Can I update a parent state from within a Dialog in Flutter?

How can I decrement a field value from firestore after submitting a form successfully?

Flutter Firestore - How to get data from a Document Reference in a Document Field?

How can I get my firebase listener to load data to my redux state in a React Native app so I can read the data within my ComponentDidMount function?

How can i get this JSON value using BeautifulSoup from window.__INITIAL_STATE__

How can I get just numbers in a field parsed to another field?

How can I get a list of a specified field from a Sails JS Waterline collection?

how can I get the "value" field from the pivot table (product to "value")? -Laravel

Why I can't set state to Firebase's doc data?

Why I can't set state to Firebase's doc data?

In Firestore how to update a field in a document that may or may not exist?

how can check int field exist or not in firebase

How can I get component state change in another component with litElement?

Redux Toolkit createEntityAdapter: how can i get current state of adapter?

How can I get the creator of a State for Hyperledger Fabric in chaincode?

How can I rename a project folder from within Visual Studio?

How can I access an outside attribute from within a Thread

How can I call lanes with parameters from within my lane?

How can I create stored procedure from within Python script?

How can I render a partial view from within a layout file?

Does a Java method exist that can "finalize" private state of Object?

ELASTICSEARCH - How can i get an aggregation in a boolean field?

ReactJS->How do i populate my state with the information from within a database?

How can I update a field in a document when another is created in cloud firestore?

How can I get a value from an array?