簡體   English   中英

未處理的異常:將對象轉換為可編碼對象失敗:攝影

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

我的 cloud_firestore 看起來像這樣:

all_images:{imageUrl:[]}, CouplePhoto:String, 女性: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>))));

如果您使用的是 json_serialization 庫,請小心。 有時在您的情況下生成的方法“_$AllImagesToJson()”無法正常工作。 看看里面。 我知道 json_serialization 庫在通用對象方面無法正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM