【问题标题】:Cast a list into a list of objects in dart将列表投射到 dart 中的对象列表中
【发布时间】:2020-06-02 05:01:39
【问题描述】:

下面的代码对node.js服务器进行了一次rest调用,它返回一个包含一系列值的json,这些值必须通过在一个Cantiere中转换值来插入到一个列表中 类型的对象。我该怎么做呢? 我想要做的是将每一个 json 转换成一个构造类型的对象,然后将它们插入到一个列表中。

JSON 调用颤振:

      static Future<List> Ricerca(Utente u, int IdCantiere, String NomeCantiere,
          String RagioneSociale, bool isUtente) async {
        var ret;
        Map map = {
          'IdUtente': u.GetIdUtente(),
          'IdCantiere': IdCantiere,
          'NomeCantiere': "a",
          'RagioneSociale': RagioneSociale,
          'CheckBoxCantieriCreatiDaUtenteLoggato': isUtente
        };
        String value = await apiRequest("/cantieri/ricerca", map);

//The ret variable holds the json, what I want to do is create a list of Cantiere type objects.
        ret = json.decode(value); 
        return (ret as Iterable<dynamic> ?? const <dynamic>[])
    .map((dynamic jsonObject) => Cantiere(
          new Cliente(
              jsonObject["IdCliente"] as int,
              jsonObject["RagioneSociale"] as String,
              jsonObject["Filiale"] as String,),
          jsonObject["IdCantiere"] as int,
          jsonObject["NomeCantiere"] as String,
          jsonObject["DescrizioneEstesa"] as String,
          jsonObject["Tipologia"] as String,
          jsonObject["Stato"] as String,
          jsonObject["StatoFatturazione"] as String,
          jsonObject["DataCreazione"] as String,
        ))
    .toList();
      }

Cantiere.dart 和 Cliente.dart

    class Cliente {
          int _IdCliente;
          String _RagioneSociale;
          String _Filiale;
          Cliente(this._IdCliente, this._RagioneSociale, this._Filiale);


          String GetRagioneSociale() {
            return this._RagioneSociale;
          }
        }
            //Classe che rappresenta il cantiere
class Cantiere {

  String _NomeCantiere,
      _Tipologia,
      _Stato,
      _StatoFatturazione,
      _DescrizioneEstesa,
      _DataCreazione;
  int _IdCantiere;
  Cliente _c;

  Cantiere(
      this._c,
      this._IdCantiere,
      this._NomeCantiere,
      this._DescrizioneEstesa,
      this._Tipologia,
      this._Stato,
      this._StatoFatturazione,
      this._DataCreazione);

            }

JSON 示例:

[
    {
        "IdCantiere": 4,
        "IdCliente": 40,
        "Filiale": "SEDE",
        "RagioneSociale": "Ca asas",
        "NomeCantiere": "sala dd",
        "DataCreazioneCantiere": "2017-08-04T18:20:31.333Z",
        "Tipologia": "Consuntivo",
        "StatoCantiere": "Chiuso",
        "StatoFatturazione": 1,
        "DescrizioneEstesa": "asasas"
    },

        "IdCantiere": 5,
        "IdCliente": 40,
        "Filiale": "SEDE",
        "RagioneSociale": "Ca asas",
        "NomeCantiere": "sala dd",
        "DataCreazioneCantiere": "2017-08-04T18:20:31.333Z",
        "Tipologia": "Consuntivo",
        "StatoCantiere": "Chiuso",
        "StatoFatturazione": 1,
        "DescrizioneEstesa": "asasas"
    }

]

【问题讨论】:

    标签: json rest flutter dart


    【解决方案1】:

    jsonDecode 所做的实际上是将 JSON 字符串转换为包含原始类型的 Lists 和 Maps 序列,具体取决于它是 JSON 数组、JSON 对象还是 JSON 原始类型。

    所以,输出的类型(基于提供的 JSON sn-p)是List,其中包含多个Maps(但并不是说类型真的是List&lt;Map&gt;)。

    要获得想要的结果,需要将解码后的 JSON 中的每个 JSON 对象映射到所需的对象类型:

    return (ret as Iterable<dynamic> ?? const <dynamic>[])
      .map((dynamic jsonObject) => 
        Cliente(
          jsonObject["IdCliente"] as int, 
          jsonObject["RagioneSociale"] as String, 
          jsonObject["Filiale"] as String,
        )
      )
      .toList();
    

    【讨论】:

    • 但是我的 Cantiere 对象也是由 Cliente 对象组成的。如何将您的推理应用于我的代码?那么解码应该去掉?
    • 扩展解析逻辑以支持选择的模型。答案中提供了如何解析jsonDecode 结果的示例。构造 Cantiere 对象而不是 Cliente 并使用从 jsonObject 检索的临时字段,这应该可以解决问题。
    • 我在上面的代码中添加了你的推理,但是当我运行它时出现以下错误:未处理的异常:类型'int'不是类型转换中'String'类型的子类型
    • 这可能是因为在Cantiere 类变量_StatoFatturazione 的类型为String,而在名为StatoFatturazione 的JSON 字段中是整数类型的JSON 原语。您可以像这样解决它:(jsonObject["StatoFatturazione"] as int).toString() 或通过将_StatoFatturazione 变量的类型更改为int 而不是将其保留为String。错误消息本身清楚地表明将int 类型的变量转换为String 类型是不可能的
    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2018-11-23
    • 1970-01-01
    • 2015-12-12
    相关资源
    最近更新 更多