【问题标题】:Parsing from json call to map in flutter从json调用解析到flutter中的map
【发布时间】:2020-02-17 15:25:50
【问题描述】:

你好flutter中下面的dart代码,对一个node.js服务器进行rest调用,返回一个map中值的map。但是,当我运行代码时,会引发以下异常。我该如何解决这个问题? 下面是node.js中从rest服务器返回的json值示例。

飞镖代码:

  static Future<Map> Ricerca(Utente u, int IdCantiere, String NomeCantiere,
      String RagioneSociale, bool isUtente) async {

    Map ret;
    Map map = {
      'IdUtente': u.GetIdUtente(),
      'IdCantiere': IdCantiere,
      'NomeCantiere': NomeCantiere,
      'RagioneSociale': RagioneSociale,
      'CheckBoxCantieriCreatiDaUtenteLoggato': isUtente
    };
    String value = await apiRequest("/cantieri/ricerca", map);

    ret = json.decode(value);
          return ret;
  }

例外:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<dynamic, dynamic>'
#0      CantiereController.Ricerca (package:MyApp/Controller/Cantiere.dart:28:5)
<asynchronous suspension>
#1      Cantiere.ricerca (package:MyApp/Model/Cantiere.dart:18:142)
#2      inizializzaValori (package:MyApp/View/Cantieri/training_screen.dart:25:38)
<asynchronous suspension>
#3      _TrainingScreenState.initState (package:MyApp/View/Cantieri/training_screen.dart:47:5)
#4      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
#5      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
#6      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#7      MultiChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5551:32)
#8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:2988<…>MyApp

ApiRest.dart 代码:

Future<String> apiRequest(String urlpassed, Map jsonMap) async {
  //Give server and port
  String server = await Storage.leggi("Server");
  String porta = await Storage.leggi("Porta");

  var url=""+server+":"+porta;

  url=url+urlpassed;
  HttpClient httpClient = new HttpClient();

  HttpClientRequest request = await httpClient.postUrl(Uri.parse(url));
  request.headers.set('content-type', 'application/json');
  request.add(utf8.encode(json.encode(jsonMap)));
  HttpClientResponse response = await request.close();

  String reply = await response.transform(utf8.decoder).join();
  httpClient.close();
  return reply;
}

JSON 示例:

[
    {
        "IdCantiere": 4,
        "IdCliente": 40,
        "Filiale": "SEDE",
        "RagioneSociale": "dsdso",
        "NomeCantiere": "sala sdsd",
        "DataCreazioneCantiere": "2017-08-04T18:20:31.333Z",
        "Tipologia": "Consuntivo",
        "StatoCantiere": "Chiuso",
        "StatoFatturazione": 1,
        "DescrizioneEstesa": "sddsdsd"
    },
    {
        "IdCantiere": 5,
        "IdCliente": 204,
        "Filiale": "SEDE",
        "RagioneSociale": "sdsds",
        "NomeCantiere": "manutenzione allarme",
        "DataCreazioneCantiere": "2017-08-04T18:27:42.017Z",
        "Tipologia": "Consuntivo",
        "StatoCantiere": "Chiuso",
        "StatoFatturazione": 1,
        "DescrizioneEstesa": "manutenzione impianto allarme AVS"
    },
    {
        "IdCantiere": 6,
        "IdCliente": 140,
        "Filiale": "SEDE",
        "RagioneSociale": "Psdsddo",
        "NomeCantiere": "Ristrutturazione terremoto",
        "DataCreazioneCantiere": "2017-08-07T17:45:15.347Z",
        "Tipologia": "Consuntivo",
        "StatoCantiere": "Chiuso",
        "StatoFatturazione": 1,
        "DescrizioneEstesa": "        stampato 10/01/19"
    }
]

【问题讨论】:

  • 用意大利语编写代码会让任何人帮你度过难关。

标签: rest flutter dart


【解决方案1】:

试试

Map<String, dynamic> ret = json.decode(value);

【讨论】:

  • 你能提供一个await apiRequest("/cantieri/ricerca", map);回复的例子吗?
  • 我的意思是从请求返回然后传递给json.decoded的json值
  • 尝试列表> ret = json.decode(value);
  • 我试过你的解决方案我有这个错误:ui_dart_state.cc(157)] 未处理的异常:类型'List'不是类型'List>的子类型'
  • 如果你这样做,它可能会工作var ret = json.decode(value);
【解决方案2】:

其余服务返回Map 列表,而您的方法正在等待Map,可能像下面的代码一样更改您的方法可能会有所帮助


static Future<List<Map<String,dynamic>>> Ricerca(Utente u, int IdCantiere, String NomeCantiere,
      String RagioneSociale, bool isUtente) async {

    Map ret;
    Map map = {
      'IdUtente': u.GetIdUtente(),
      'IdCantiere': IdCantiere,
      'NomeCantiere': NomeCantiere,
      'RagioneSociale': RagioneSociale,
      'CheckBoxCantieriCreatiDaUtenteLoggato': isUtente
    };
    String value = await apiRequest("/cantieri/ricerca", map);

    ret = json.decode(value);
          return ret;
  }

【讨论】:

  • 我试过你的解决方案我有这个错误:ui_dart_state.cc(157)] 未处理的异常:类型'List'不是类型'List>的子类型'
  • 试试这个建议,把'List&lt;Map&lt;String, dynamic&gt;&gt;换成List&lt;dynamic&gt;,会发生什么?
【解决方案3】:

您的 JSON 返回的是列表而不是地图。保留一个列表:

List<dynamic> ret = json.decode(value);

然后你可以改变列表类型:

List<Map<String, dynamic>> data = ret.map((value) => value as Map<String, dynamic>).toList();

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 2019-11-08
    • 2021-09-01
    • 2019-08-19
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多