flutter - 未处理的异常:将对象转换为可编码对象失败:摄影
问题描述
我的 cloud_firestore 看起来像这样:
all_images:{imageUrl:[]}, couplePhoto:String, female:String, image_url:String, info:String, male:String
我的模型课:
import 'package:json_annotation/json_annotation.dart';
part 'Model.g.dart';
@JsonSerializable()
class Photography {
final List<AllImages> all_images;
final String couplePhoto;
final String female;
final String image_url;
final String info;
final String male;
Photography(this.all_images, this.couplePhoto, this.female, this.image_url,
this.info, this.male);
factory Photography.fromJson(Map<String, dynamic> json) =>
_$PhotographyFromJson(json);
Map<String, dynamic> toJson() => _$PhotographyToJson(this);
}
@JsonSerializable()
class AllImages {
final List<String> imageUrl;
AllImages(this.imageUrl);
factory AllImages.fromJson(Map<String, dynamic> json) =>
_$AllImagesFromJson(json);
Map<String, dynamic> toJson() => _$AllImagesToJson(this);
}
目前,我还没有 adminPanel 来插入数据。所以,现在我需要读取数据。运行此数据库时,此处出现错误。
Future<Photography> postsMap = Firestore.instance
.collection('photography')
.document("0yUc5QBGHNNq6WK9CyyF")
.setData(jsonDecode(jsonEncode(Photography)));
解决方案
在这一行:
.setData(jsonDecode(jsonEncode(Photography)));
您正在引用 Photo 类的 Type 对象,而不是您需要传递一个实例(因此您需要使用所需的值调用构造函数,并传递结果)。
.setData(jsonDecode(jsonEncode(Photography(<pass-in-constructor-args-here>))));
推荐阅读
- android - 如何使用 MaterialComponents 正确显示 DatePickerDialog?
- r - 用函数评估多项式
- c# - UI 自动化:打开文件对话框元素树不包含所有元素
- javascript - 使用手机时的结果与使用 PC 时不同
- firebase - 可以在没有冷启动的情况下将 Firebase 云功能用于 SSR?
- webdriver - 为什么在 Selenium 下的 Chrome 开发人员工具中没有显示对 iFrame 的网络请求?
- testng - 在 TestNG 中绑定带有超时事件的方法
- json - RDBMS 存储为 json 文件 Firebase
- docker - Treafik 路径路由
- c - 使用 Xamarin.forms 启动外部应用程序