Reputation: 1715
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"
}
]
Upvotes: 0
Views: 979
Reputation: 824
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();
Upvotes: 0
Reputation: 1315
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;
}
Upvotes: 0