简体   繁体   English

未处理的异常:将对象转换为可编码对象失败:摄影

[英]Unhandled Exception: Converting object to an encodable object failed: Photography

my cloud_firestore looks like this:我的 cloud_firestore 看起来像这样:

all_images:{imageUrl:[]}, couplePhoto:String, female:String, image_url:String, info:String, male:String all_images:{imageUrl:[]}, CouplePhoto:String, 女性:String, image_url:String, info:String,male:String

my model class:我的模型类:

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);
}

Currently, I haven't adminPanel to data insert.目前,我还没有 adminPanel 来插入数据。 so, now I need to read data.所以,现在我需要读取数据。 When running this database, the error is coming in here.运行此数据库时,错误出现在这里。

    Future<Photography> postsMap = Firestore.instance
        .collection('photography')
        .document("0yUc5QBGHNNq6WK9CyyF")
        .setData(jsonDecode(jsonEncode(Photography)));

At this line:在这一行:

.setData(jsonDecode(jsonEncode(Photography)));

You are referencing the Type object for the Photograph class, instead you need to pass an instance (so you would need to call the constructor with the required values, and pass the result of that).您正在引用 Photo 类的 Type 对象,而您需要传递一个实例(因此您需要使用所需的值调用构造函数,并传递其结果)。

.setData(jsonDecode(jsonEncode(Photography(<pass-in-constructor-args-here>))));

if you are using json_serialization library be careful.如果您使用的是 json_serialization 库,请小心。 sometimes the generated method in your case "_$AllImagesToJson()" not working properly.有时在您的情况下生成的方法“_$AllImagesToJson()”无法正常工作。 look inside this.看看里面。 I know that json_serialization library is not working properly in terms of generic objects我知道 json_serialization 库在通用对象方面无法正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:将 object 转换为可编码的 object 失败:_LinkedHashMap - Unhandled Exception: Converting object to an encodable object failed: _LinkedHashMap 未处理的异常:将 object 转换为可编码的 object 失败:“XFile”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'XFile' Flutter:未处理的异常:将对象转换为可编码对象失败:“AddProjectModel”的实例 - Flutter: Unhandled Exception: Converting object to an encodable object failed: Instance of 'AddProjectModel' 未处理的异常:将 object 转换为可编码的 object 失败:“测量”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'Mesure' 未处理的异常:将对象转换为可编码对象失败:“产品”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'Product' 如何解决未处理的异常:将对象转换为可编码对象失败? - how to solve Unhandled Exception: Converting object to an encodable object failed? 未处理的异常:将 object 转换为可编码的 object 失败:“LoginModel”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'LoginModel' 未处理的异常:将 object 转换为可编码的 object 失败:“日期时间”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'DateTime' 未处理的异常:将 object 转换为可编码的 object 失败:“TextEditingController”实例 - Unhandled Exception: Converting object to an encodable object failed: Instance of 'TextEditingController' 我在 flutter 中收到错误“未处理的异常:将 object 转换为可编码的 object 失败:Null” - I get the error 'Unhandled Exception: Converting object to an encodable object failed: Null' in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM