簡體   English   中英

在 Flutter 中將對象轉換為可編碼對象失敗

[英]Converting object to an encodable object failed in Flutter

我在這里的示例中創建了一個類: https ://docs.flutter.dev/development/data-and-backend/json

class NewUser {
String Username;
String Mail;
String Password;
Lang Language;

NewUser(this.Username, this.Mail, this.Password, this.Language);

NewUser.fromJson(Map<String, dynamic> json)
  : Username = json['Username'],
    Mail = json['Mail'],
    Password = json['Password'],
    Language = json['Language'];

Map<String, dynamic> toJson() => {
    'Username': Username,
    'Mail': Mail,
    'Password': Password,
    'Language': Language
};
}

當我嘗試調用 toJson 時失敗:將對象轉換為可編碼對象失敗:'NewUser' 的實例

NewUser user = NewUser('username', 'mail', 'password', Lang.cs);
String json = jsonEncode(user);
print(json);

知道如何解決這個問題嗎?

編輯:這是因為“語言”是枚舉。 如何序列化枚舉?

您可以使用Lang.name編碼為 JSON 和Lang.values.byName(...)從 JSON 解碼。 看看下面:


class NewUser {
  String Username;
  String Mail;
  String Password;
  Lang Language;

  NewUser(this.Username, this.Mail, this.Password, this.Language);

  NewUser.fromJson(Map<String, dynamic> json)
    : Username = json['Username'],
      Mail = json['Mail'],
      Password = json['Password'],
      Language = Lang.values.byName(json['Language']);

  Map<String, dynamic> toJson() => {
      'Username': Username,
      'Mail': Mail,
      'Password': Password,
      'Language': Language.name,
  };
}

Flutter - 將 map 的 Json map 轉換為列表<object>在 flutter<div id="text_translate"><p> 我正在嘗試在父 class 中反序列化 json。 我的其中一個項目是 Map 的 Map,我想將其反序列化為 class 類型。 由於 map 中嵌套的 map ,我正在努力解析它,我不清楚正確的語法。 歸根結底,我想要父 class 中的列表。 json 類型塊中的項目是動態的,因此可能存在具有不同描述的關鍵、通知等類型。</p><p> <strong>Json 樣品:</strong></p><pre> { "types":{ "alert":{ "description":"Action item." }, "question":{ "description":"Select an applicable response." }, "info":{ "description":"This is an information message, no action required." } } }</pre><p> <strong>class 類型:</strong></p><pre> class Types { final String name; final String description; Types({ required this.name, required this.description, }); factory Types.fromJson(String id, Map&lt;String, dynamic&gt; json) { return Types( name: id, description: json['description'] == null? '': json['description'], ); } }</pre><p> <strong>父 class:</strong></p><pre> class Parent { final String id; final String name; final String description; final Features features; final List&lt;Types&gt; types; final List&lt;String&gt; users; Parent({ required this.id, required this.name, required this.description, required this.features, required this.types, required this.users, }); factory Parent.fromJson( Map&lt;String, dynamic&gt; json) { return Parent( id: json['id'] == null? '': json['id'], name: json['name'] == null? '': json['name'], description: json['description'] == null? '': json['description'], features: json['features'] == null? Features(): Features.fromJson(json['features']), types: json['types'] == null? []: //??? How to deserialize Map{Map{}}, users: json['users'] == null? []: List&lt;String&gt;.from(json['users']), ); } }</pre><p> 任何和所有的幫助表示贊賞。 同樣,如果有更好的方法來存儲這些數據,我對此持開放態度。 class 類型允許我在必要時添加未來的字段。</p><p> 謝謝你。</p></div></object>

[英]Flutter - Converting Json map of a map to List<Object> in flutter

暫無
暫無

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

相關問題 將 object 轉換為可編碼的 object 失敗 將 object 轉換為可編碼的 object 失敗:“偏移”實例 將 object 轉換為可編碼 object 失敗:model 的實例 首先:將 object 轉換為可編碼的 object 失敗 - 然后:HydratedCubit 未水合 未處理的異常:將 object 轉換為可編碼的 object 失敗:“XFile”實例 如何在顫動中將文件對象轉換為可編碼的 json 對象? Map <int, list<int> &gt; 轉換為可編碼 object 失敗</int,> 問題將 json 響應轉換為 Flutter 中的 object Flutter - 將 map 的 Json map 轉換為列表<object>在 flutter<div id="text_translate"><p> 我正在嘗試在父 class 中反序列化 json。 我的其中一個項目是 Map 的 Map,我想將其反序列化為 class 類型。 由於 map 中嵌套的 map ,我正在努力解析它,我不清楚正確的語法。 歸根結底,我想要父 class 中的列表。 json 類型塊中的項目是動態的,因此可能存在具有不同描述的關鍵、通知等類型。</p><p> <strong>Json 樣品:</strong></p><pre> { "types":{ "alert":{ "description":"Action item." }, "question":{ "description":"Select an applicable response." }, "info":{ "description":"This is an information message, no action required." } } }</pre><p> <strong>class 類型:</strong></p><pre> class Types { final String name; final String description; Types({ required this.name, required this.description, }); factory Types.fromJson(String id, Map&lt;String, dynamic&gt; json) { return Types( name: id, description: json['description'] == null? '': json['description'], ); } }</pre><p> <strong>父 class:</strong></p><pre> class Parent { final String id; final String name; final String description; final Features features; final List&lt;Types&gt; types; final List&lt;String&gt; users; Parent({ required this.id, required this.name, required this.description, required this.features, required this.types, required this.users, }); factory Parent.fromJson( Map&lt;String, dynamic&gt; json) { return Parent( id: json['id'] == null? '': json['id'], name: json['name'] == null? '': json['name'], description: json['description'] == null? '': json['description'], features: json['features'] == null? Features(): Features.fromJson(json['features']), types: json['types'] == null? []: //??? How to deserialize Map{Map{}}, users: json['users'] == null? []: List&lt;String&gt;.from(json['users']), ); } }</pre><p> 任何和所有的幫助表示贊賞。 同樣,如果有更好的方法來存儲這些數據,我對此持開放態度。 class 類型允許我在必要時添加未來的字段。</p><p> 謝謝你。</p></div></object> Flutter 從 Json 轉換嵌套對象返回 null
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM