I am trying to get the JSON response from the server and output it to the console.
Future login() async {
var response = await http.get(
You can use
new Map<String, dynamic>.from(snapshot.value);
You have to convert the runtimeType
of data
from _InternalLinkedHashMap
to an actual List
.
One way is to use the List.from
.
final _data = List<dynamic>.from(
data.map<dynamic>(
(dynamic item) => item,
),
);
You can get this error if you are using retrofit.dart and declare the wrong return type for your annotated methods:
@GET("/search")
Future<List<SearchResults>> getResults();
// wrong! search results contains a List but the actual type returned by that endpoint is SearchResults
vs
@GET("/search")
Future<SearchResults> getResults();
// correct for this endpoint - SearchResults is a composite with field for the list of the actual results
If you need work with generic fields has a workaround:
class DicData
{
int tot;
List<Map<String, dynamic>> fields;
DicData({
this.tot,
this.fields
});
factory DicData.fromJson(Map<String, dynamic> parsedJson) {
return DicData(
tot: parsedJson['tot'],
//The magic....
fields : parsedJson["fields"] = (parsedJson['fields'] as List)
?.map((e) => e == null ? null : Map<String, dynamic>.from(e))
?.toList()
);
}
}
This worked for me:
setState(){
Map<String, dynamic> map = json.decode(response.body);
Data = map["name"];
}
// for printing
Data[index]['name1'].toString(),
I had the same error using json_annotation
, json_serializable
, build_runner
. It occurs when calling the ClassName.fromJson()
method for a class that had a class property (example: class User has a property class Address).
As a solution, I modified the generated *.g.dart
files of each class, by changing Map<String, dynamic>)
to Map<dynamic, dynamic>)
in everywhere there is a deep conversion inside the method _$*FromJson
The only problem is that you have to change it again every time you regenerate your files.