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,
);
}
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'] ?? "",
);
}
}
StreamProvider
. I'm gonna edit the question for you. – Ezequielrestaurant
collection, this is not being generated by a missing doc. There might be a document where theisfeatured
field either does not exist or is empty/null. In order to check that, please log all documents that you have to check this – Hunch