0

Hi the following dart code in flutter, make a rest call to a node.js server returning a map of values ​​in a map. When I run the code, however, the exception below is raised. How can I go about solving this? Below is an example of json values ​​returning from the rest server in node.js.

Dart Code:

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

Exception:

[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 Code:

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 Example:

[
    {
        "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"
    }
]
1
  • Having your code in Italian is going to make any one helping you have a very hard time. Commented Feb 17, 2020 at 15:30

3 Answers 3

1

Try

Map<String, dynamic> ret = json.decode(value);
Sign up to request clarification or add additional context in comments.

6 Comments

Can you provide an example of the response of await apiRequest("/cantieri/ricerca", map); ?
I meant the json value that is returned from the request and then passed to json.decoded
Try List<Map<String, dynamic>> ret = json.decode(value);
I tried your solution I have this error: ui_dart_state.cc(157)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
If you do this it'll probably work var ret = json.decode(value);
|
0

The rest service is returning a list of Map and you method is awaiting for just a Map, probably change your method like the code below could help


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

2 Comments

I tried your solution I have this error: ui_dart_state.cc(157)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
Try with the suggestion, replace 'List<Map<String, dynamic>> with List<dynamic> , what happens?
0

Your JSON is returning a List not a Map. Keep it a List:

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

Then you can change the list type:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.