Firebase Flutter Riverpod:映射模型时如何获取Firebase自动生成的documentID
修复方案
你遇到的报错本质是两个问题导致的:
- Firebase自动生成的documentID不存在于文档字段数据中,它是
QueryDocumentSnapshot对象本身自带的id属性,不会出现在e.data()返回的Map里 - 工厂构造方法中你写的
restaurantModel[documentID]是试图用还未初始化的类实例属性documentID作为key取值,自然会触发报错
第一步:修改Model层工厂构造
给fromFirebase方法新增id入参,用来接收Service层传递的文档ID:
import 'package:cloud_firestore/cloud_firestore.dart'; class RestaurantModel { final String? name; final String? phone; final String? fullAddress; final String? yearsInBusiness; final String? priceRange; final String? websiteLink; final String? documentID; RestaurantModel({ required this.name, required this.phone, required this.fullAddress, required this.yearsInBusiness, required this.priceRange, required this.websiteLink, required this.documentID, }); factory RestaurantModel.fromFirebase(Map<String, dynamic> restaurantModel, String documentId) { return RestaurantModel( name: restaurantModel['name'], phone: restaurantModel['phone'], fullAddress: restaurantModel['fullAddress'], yearsInBusiness: restaurantModel['yearsInBusiness'], priceRange: restaurantModel['priceRange'], websiteLink: restaurantModel['websiteLink'], // 直接用传入的id赋值即可 documentID: documentId, ); } }
第二步:修改Service层映射逻辑
调用工厂方法时,把QueryDocumentSnapshot自带的id属性作为第二个参数传入:
Stream<List<RestaurantModel>> restaurantModel() { return _firebase.collection('restaurantsPensacola').snapshots().map((event) => event.docs.map((e) => RestaurantModel.fromFirebase(e.data(), e.id)).toList() ); }
可选优化方案:直接传递整个快照对象到工厂方法,代码更简洁
Model层工厂构造修改为:factory RestaurantModel.fromFirebase(QueryDocumentSnapshot snapshot) { Map<String, dynamic> data = snapshot.data() as Map<String, dynamic>; return RestaurantModel( name: data['name'], phone: data['phone'], fullAddress: data['fullAddress'], yearsInBusiness: data['yearsInBusiness'], priceRange: data['priceRange'], websiteLink: data['websiteLink'], documentID: snapshot.id, ); }对应Service层调用改为:
event.docs.map((e) => RestaurantModel.fromFirebase(e)).toList()
内容的提问来源于stack exchange,提问作者DavesMobileApps
相关产品推荐
相关产品推荐

