Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist - Flutter
Asked Answered
E

1

0

I want to check if there is a field named isfeatured exists in Doc of collection? I get below error if certain feild doesn't exist in the document of firestore database, even though I'm tackling it with ?? as shown below.

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

Below is how I'm getting data from firestore


Stream<List<Restaurant>> get getAllRestaurants => FirebaseFirestore.instance.collection('restaurants').snapshots().map(_restaurantsDataFromSnapshot);

// Mapping Restaurant data
List<Restaurant> _restaurantsDataFromSnapshot(QuerySnapshot snap) {
    List<Restaurant> restaurantList = [];
    snap.docs.forEach((element) {
     restaurantList.add(Restaurant.fromJson(element));
    });
    return restaurantList;
  }

Only fromJson function of Restaurant class. Most people are suggesting to use exists for field but I can't use it for field. It is for entire QueryDocumentSnapshot. fromJson is what map the data.

 factory Restaurant.fromJson(QueryDocumentSnapshot map) {

    return Restaurant(
      // Getting other feilds
      isFeatured: map.data()['isFeatured'] ?? false,
    );
  }

enter image description here

EDIT: I'm using stream with Streambuilder like that

class MainClass extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    
    StreamProvider restaurantsProvider = StreamProvider<List<Restaurant>>.value(
      initialData: [],
      value: DatabaseService().getAllRestaurants,
    );
    return MultiProvider(
      providers: [
        restaurantsProvider,
        // Some other providers
      ],
      child: HomeView(),
    );
  }
}

Full Restaurant class if someone is interested

import 'package:cloud_firestore/cloud_firestore.dart';
class Restaurant {   
  String restaurantID;   
  String name;   
  double rating;   
  String availabilityTime;   
  int orders;   
  List<String> restaurantCtg;   
  String imageUrl;   
  bool isFeatured;   
  String address;
  bool availableStatus;   
  String coordinates;

  Restaurant({
    this.restaurantID,
    this.name,
    this.rating,
    this.availabilityTime,
    this.orders,
    this.restaurantCtg,
    this.imageUrl,
    this.isFeatured,
    this.address,
    this.availableStatus,
    this.coordinates,   });

  Map<String, dynamic> toMap() {
    return {
      'restaurantID': restaurantID,
      'name': name,
      'rating': rating,
      'availabilityTime': availabilityTime,
      'orders': orders,
      'restaurantCtg': restaurantCtg,
      'imageURL': imageUrl,
      'isFeatured': isFeatured,
      'address': address,
      'availableStatus': availableStatus,
      'coordinates': coordinates,
    };   }

  factory Restaurant.fromJson(QueryDocumentSnapshot map) {

    return Restaurant(
      restaurantID: map.data()['restaurantID'] ?? "",
      name: map.data()['name'] ?? "",
      rating: map.data()['rating'] ?? 0.0,
      orders: map.data()['orders'] ?? 0,
      restaurantCtg: List<String>.from(map.data()['restaurantCtg']),
      imageUrl: map.get('imageURL').exist ? map.data()['imageURL'] : "",
      isFeatured: map.data()['isFeatured'] ?? false,
      address: map.data()['address'] ?? "",
      availabilityTime: map.data()['availabilityTime'] ?? "09:00-18:00",
      availableStatus: map.data()['availableStatus'] ?? true,
      coordinates: map.data()['coordinates'] ?? "",
    );   
  } 
}
Ezequiel answered 26/8, 2021 at 11:21 Comment(3)
Are you using the stream in a StreamBuilder?Manda
@VictorEronmosele I forgot to mention that I'm using this stream with StreamProvider. I'm gonna edit the question for you.Ezequiel
As you are fetching all document from the restaurant collection, this is not being generated by a missing doc. There might be a document where the isfeatured field either does not exist or is empty/null. In order to check that, please log all documents that you have to check thisHunch
C
1

I guess it is not about null value or not So your code:

isFeatured: map.data()['isFeatured'] ?? false

the code map.data()['isFeatured'] has neither boolean value nor null value so I found that you may check if a parameter exists this way:

isFeatured: map.data().containsKey('isFeatured') 
? map.data()['isFeatured'] : false
Coadjutress answered 31/8, 2021 at 15:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.