首页 > 解决方案 > 错误状态:无法获取 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'] ?? "",
    );   
  } 
}

标签: flutterdartgoogle-cloud-firestorestreamsnapshot

解决方案


我想这与空值无关所以你的代码:

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

代码map.data()['isFeatured']既没有布尔值也没有值,所以我发现您可以通过这种方式检查参数是否存在:

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

推荐阅读