Firebase Flutter Riverpod:在使用 Riverpod 跨模型映射值时,如何从 Firebase 获取 documentID?包含代码

Firebase Flutter Riverpod: How do I get the documentID from Firebase when using Riverpod to map values across a model? Code Included

我需要在 Firebase 集合中获取我的文档的文档 ID。 documentID 不存储为文档中的字段。 ID 由 Firebase 在导入时选择。

名称、phone、完整地址等其他字段都显示完美。如何使用我的 Map/Model 进入自动生成的 docID?

非常感谢任何指导。

服务:

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'restaurant_model.dart';

final restaurantProvider = StreamProvider.autoDispose((ref) {
  final service = ref.watch(restaurantServiceProvider);
  return service.restaurantModel();
});

final restaurantServiceProvider = Provider<RestaurantService>((ref) {
  final firebase = FirebaseFirestore.instance;
  return RestaurantService(firebase);
});

class RestaurantService {
  final FirebaseFirestore _firebase;
  
  RestaurantService(this._firebase);
  
  Stream<List<RestaurantModel>> restaurantModel() {
    return _firebase.collection('restaurantsPensacola').snapshots().map((event) => event.docs.map((e) => RestaurantModel.fromFirebase(e.data())).toList());
  }
}

型号

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) {

    return RestaurantModel(
        name: restaurantModel['name'],
        phone: restaurantModel['phone'],
        fullAddress: restaurantModel['fullAddress'],
        yearsInBusiness: restaurantModel['yearsInBusiness'],
        priceRange: restaurantModel['priceRange'],
        websiteLink: restaurantModel['websiteLink'],
        //THIS IS WHERE I AM LOST AS THIS GENERATES ERROR
        //Instance members can't be accessed from a factory constructor.
        documentID: restaurantModel[documentID],
    );
  }
}

文档 ID 可以通过 e.id 访问。对于您的 RestaurantModel,您可能希望将其重命名为 restaurantId 以更准确地描述它所描述的内容,并在创建时将自动生成的文档 ID 保存为 restaurantId。您还可以生成自己的唯一 ID(使用 uuid 包或类似的东西)并使用该 ID 创建一个新文档。

示例:

    Future<dynamic> createRestaurantRecord(
      String id, Map<String, dynamic> data) async {
    return await _firestore.doc('restaurants/$id').set(data);
    }

您的工厂将无法工作,因为 documentID 不会自动与文档的其余数据一起存储。

我能够与我的导师见面,并被证明我可以通过在 e.data 之后添加 e.id 从 Firebase 获取 ID。

    Stream<List<RestaurantModel>> restaurantModel() {
    return _firebase.collection('restaurantsPensacola').snapshots().map(
        (event) => event.docs.map((e) => RestaurantModel.fromFirebase(e.data(), e.id)).toList());
  }

然后,建议我稍微更改名称以便更容易区分值。您会看到我是如何合并来自 Firebase 的 documentID 的。

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? id;

  RestaurantModel({
    required this.name,
    required this.phone,
    required this.fullAddress,
    required this.yearsInBusiness,
    required this.priceRange,
    required this.websiteLink,
    required this.id,
  });

  factory RestaurantModel.fromFirebase(
      Map<String, dynamic> restaurantModel, String documentIDFromFirebase) {
    return RestaurantModel(
      name: restaurantModel['name'],
      phone: restaurantModel['phone'],
      fullAddress: restaurantModel['fullAddress'],
      yearsInBusiness: restaurantModel['yearsInBusiness'],
      priceRange: restaurantModel['priceRange'],
      websiteLink: restaurantModel['websiteLink'],
      id: documentIDFromFirebase,
    );
  }
}