【问题标题】:Unhandled Exception: Converting object to an encodable object failed: Photography未处理的异常:将对象转换为可编码对象失败:摄影
【发布时间】:2019-11-06 02:42:30
【问题描述】:

我的 cloud_firestore 看起来像这样:

all_images:{imageUrl:[]}, couplePhoto:String, female:String, image_url:字符串,信息:字符串,男性:字符串

我的模型类:

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

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    在这一行:

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

    您正在引用 Photo 类的 Type 对象,而不是您需要传递一个实例(因此您需要使用所需的值调用构造函数,并传递其结果)。

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

    【讨论】:

    • 我删除了模型类的构造函数,但同样的错误即将到来
    • 我的错误。但另一个问题来了兄弟。如果有时间请查看stackoverflow.com/q/56815045/9139407
    【解决方案2】:

    如果您使用 json_serialization 库,请小心。有时在您的情况下生成的方法“_$AllImagesToJson()”无法正常工作。看看里面。我知道 json_serialization 库在通用对象方面无法正常工作

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 2020-07-18
      • 2021-10-05
      • 2021-04-08
      • 2021-04-16
      • 2018-09-20
      • 2021-11-04
      • 2020-08-21
      • 2023-04-09
      相关资源
      最近更新 更多