flutter - 错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段 - Flutter
问题描述
我想检查 isfeatured
集合文档中是否有一个名为 exists 的字段?如果 Firestore 数据库的文档中不存在某些字段,即使我正在处理它,??
如下所示,我也会收到以下错误。
错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段
以下是我从 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;
}
只有类fromJson
的功能Restaurant
。大多数人建议使用exists
字段,但我不能将其用于字段。它适用于整个QueryDocumentSnapshot
. fromJson
是什么映射数据。
factory Restaurant.fromJson(QueryDocumentSnapshot map) {
return Restaurant(
// Getting other feilds
isFeatured: map.data()['isFeatured'] ?? false,
);
}
编辑:
我正在使用Streambuilder
这样的流
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(),
);
}
}
Restaurant
如果有人感兴趣,全班
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'] ?? "",
);
}
}
解决方案
我想这与空值无关所以你的代码:
isFeatured: map.data()['isFeatured'] ?? false
代码map.data()['isFeatured']既没有布尔值也没有空值,所以我发现您可以通过这种方式检查参数是否存在:
isFeatured: map.data().containsKey('isFeatured')
? map.data()['isFeatured'] : false
推荐阅读
- python - 当记录具有特定状态时,如何使 Odoo 表单只读?
- git - 现在默认情况下 git set 'git pull' 和 rebase 吗?什么时候启动?
- angular - QueryList.changes.subscribe() 不会在 Angular 单元测试中触发
- node.js - 403 Forbidden,docker容器之间的通信
- javascript - 加载页面但未发生事件时执行的事件侦听器函数
- c++ - 调用具有相似位置的两个不同文件
- java - Gremlin P.eq 可以接受顶点吗?
- python - 如何在Python的CSV文件中打印组中出现的唯一名称
- python-3.x - 使用 request_mock 根据请求动态设置响应
- dot - 点图生成在 5000 个节点和 7000 条边附近太慢