首页 > 解决方案 > 在 Firestore 中存储参考字符串

问题描述

我有一个名为 FoodModel.dart 的模型类,如果我试图引用文件,那么该文件将存储在 Firebase 存储中,它将具有指向存储的字符串路径。此路径将作为字符串存储在 Firestore 中。

class FoodModel{ 
 final String? id;
 final String? name;
 @ImageConvertor()
 final File? file;
 
  const FoodModel({
  required this.id,
  this.name,
  this.file})
  }

这是Json转换器

class ImageConvertor implements JsonConverter<File?, String?> {
   const ImageConvertor();

   @override
   File? fromJson(String? json) => File(json!);

   @override
   String? toJson(File? object) => object!.path;
  }

但是模型不会转换为字符串到文件或文件到字符串。

当我尝试将其保存在 Firestore 中时抛出错误。无效参数:“FoodModel”实例

文件:'https:

标签: fluttergoogle-cloud-firestore

解决方案


推荐阅读