How to implement T for binding JSON in Flutter model? - json

I want to use Generic Type Parameter in Flutter, but I'm confused on how to use it with my JSON. Here's the code I've tried:
class BaseMdl {
bool status;
int http_code;
String message;
dynamic data;
BaseMdl({this.status, this.http_code, this.message, List<dynamic> data}) : data = data ?? <dynamic>[];
factory BaseMdl.fromJson(Map<String, dynamic> parsedJson) {
var list = parsedJson['data'] as List;
List<dynamic> dataList = list.map((i) => BaseMdl.fromJson(i)).toList();
return BaseMdl(
status: parsedJson['status'],
http_code: parsedJson['http_code'],
message: parsedJson['message'],
data: dataList
);
}
}
Sorry I'm a beginner in Flutter.

Related

How to send list of model objects in json format to API in flutter

I am working on a Food Delivery App in Flutter and I have a list of model objects in my cart System. I need to send this list to API in JSON format so I convert that to JSON before sending it to server but i am getting error:
errors: {: [Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Metronic.Models.FoodOrderInsert' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I am beginner in Flutter and your kind response will highly be appreciated.
I am currently converting list and sending it to API like this:
I have my List of modelbjects:
List<FoodDetail> foodOrderDetails = [...]
var jsonObjectsList = [];
//Here I convert my list of FoodDetails into list of jsonObjects
for (var item in foodOrderDetail) {
jsonObjectsList.add(item.toJson());
}
await http
.post(
Uri.parse(url),
headers: ApiURLs.header,
body: jsonEncode(jsonObjectsList)
)
And my API required data is:
[{
"OrderId": 0,
"ProductId": 0,
"Quantity": 0,
"Price": 0}]
my FoodDetail class contain same these properties and converted to json but the problem I think is in conveting whole list to json.
i suggestion to use DIO lib its automatically convert your data to json
check it: https://pub.dev/packages/dio
use this class to parse the server response
FoodExample foodExample=FoodExample.fromJson(jsonEncode(jsonObjectsList))
with this line you will have access to all the attributes of the class
class FoodExample {
int orderId;
int productId;
int quantity;
int price;
FoodExample({this.orderId, this.productId, this.quantity, this.price});
FoodExample.fromJson(Map<String, dynamic> json) {
if(json["OrderId"] is int)
this.orderId = json["OrderId"];
if(json["ProductId"] is int)
this.productId = json["ProductId"];
if(json["Quantity"] is int)
this.quantity = json["Quantity"];
if(json["Price"] is int)
this.price = json["Price"];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data["OrderId"] = this.orderId;
data["ProductId"] = this.productId;
data["Quantity"] = this.quantity;
data["Price"] = this.price;
return data;
}
}
I solved my question by using Dio package,
That was the error due to http package internal code.
My Method for posting list to Server:
Future<int> insertFoodOrderDetails(List<FoodOrderDetail> foodOrderDetail) async {
var dio = Dio();
var mapData;
var url = ApiURLs.baseUrl + ApiURLs.insertFoodOrderDetails;
var jsonObjectsList = [];
for (var item in foodOrderDetail) {
jsonObjectsList.add(item.toMap());
}
await
dio.post(
url,
queryParameters: ApiURLs.header,
data: jsonObjectsList
)
.then((value) {
mapData = value.data as Map<String, dynamic>;
});
if (mapData['Status'] == 'Inserted Successfully') {
print(mapData['Id']);
return mapData['Id'] ;
}
else{
return 0;
}
}

decode stored json result string in shared preferences

When the app launch it triggers an API call and in my case, I want to store the response data for later use, so I used shared preferences for that and stored response data as a string. now I want to properly decode the data to use, from the stored string from shared preferences.
here is how I covert the data to string,
SharedPreferences prefs = await SharedPreferences.getInstance();
Response response = await _dio.post(
_baseUrl,
data: {"index": indexNum, "password": password},
options: Options(contentType: Headers.formUrlEncodedContentType),
);
if (response.statusCode == 200) {
var result = response.data;
//convert result data to string
var resultData = Result.encode(result);
// store the resultData in shared_preferences
prefs.setString('results', resultData);
}
encode method,
class Result {
Result({
required this.table,
required this.data,
});
String table;
List<Data> data;
factory Result.fromJson(Map<String, dynamic> json) => Result(
table: json["table"],
data: List<Data>.from(json["data"].map((x) => Data.fromJson(x))),
);
//encode method
static String encode(List<dynamic> results) => json.encode(
results.map((result) => result.toString()).toList(),
);
}
here is my approach to decode data from string,
getData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? resultData = prefs.getString('results');
List<dynamic> decodedJson = jsonDecode(resultData!);
print(decodedJson);
}
resultData string,
resultData string after decode,
I am new to flutter and what I want is the proper way to decode this data from models. Below are my model classes.
import 'dart:convert';
class Result {
Result({
required this.table,
required this.data,
});
String table;
List<Data> data;
factory Result.fromJson(Map<String, dynamic> json) => Result(
table: json["table"],
data: List<Data>.from(json["data"].map((x) => Data.fromJson(x))),
);
static String encode(List<dynamic> results) => json.encode(
results.map((result) => result.toString()).toList(),
);
}
class Data {
Data({
required this.subjectName,
required this.year,
required this.credits,
required this.sOrder,
required this.result,
required this.onlineAssignmentResult,
});
String subjectName;
String year;
String credits;
String sOrder;
String result;
String onlineAssignmentResult;
factory Data.fromJson(json) => Data(
subjectName: json["subject_name"],
year: json["year"],
credits: json["credits"],
sOrder: json["s_order"],
result: json["result"],
onlineAssignmentResult: json["online_assignment_result"],
);
}
Appreciate your time and help.
Your JSON is in the wrong syntax for Flutter to decode with jsonDecode()
All strings and variable names need to be enclosed in " or '
I.e. {subject_name: should be {"subject_name":

Dart: cannot convert List<dynamic> to List<Map<String, dynamic>> despite cast

I am trying to parse data from a Rest API inside a Dart/Flutter application. The JSON contains a field called data at the root, which contains a list of Words. I want to get a List<Word> from this JSON. I already have the following code:
Map<String, dynamic> jsonMap = json.decode(jsonString);
List<Word> temp = jsonMap['data']
.map((map) => map as Map<String, dynamic>)
.map((Map<String, dynamic> map) => Word.fromJson(map)).toList(); // map to List<Word>
Word.fromJson has the following signature:
Word.fromJson(Map<String, dynamic> json)
The final call to map gives the following error:
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
From my understanding, the call to map((map) => map as Map<String, dynamic>) should convert the List<dynamic> to a List<Map<String, dynamic>>, so I am confused as to why I get the error.
Any advice appreciated.
If data is a List of words, you can "cast" to generic List and iterate each item to cast into a new Word object,
List<Word> temp = (jsonMap['data'] as List).map((itemWord) => Word.fromJson(itemWord)).toList();
The key is String, and data is Dynamic, if jsonMap['data'] is a List on jsonString, it's not a Map<String,dynamic> and can not cast direct to map.
Sample of jsonString and convert:
final jsonString = '''
{
"field": "titulo",
"data": [{"teste":1},{"teste":2},{"teste":3},{"teste":4}]
}
''';
final jsonMap = json.decode(jsonString);
List<Word> temp = (jsonMap['data'] as List)
.map((itemWord) => Word.fromJson(itemWord))
.toList();
Word Class
class Word {
int teste;
Word({this.teste});
Word.fromJson(Map<String, dynamic> json) {
teste = json['teste'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['teste'] = this.teste;
return data;
}
}
Generated classs using JSON to Dart
https://javiercbk.github.io/json_to_dart/
If you want to convert a List<dynamic> to List<Map<String, dynamic>> as the title suggests, you should cast 2 times:
(jsonDecode(response.body)["data"] as List).map((e) => e as Map<String, dynamic>)?.toList();
If you are using strong mode I had to explicitly define the field type the ? was also not necessary.
Note the 'dynamic e'
(jsonDecode(response.body)["data"] as List).map((dynamic e) => e as Map<String, dynamic>).toList();

parse json in flutter with map type

i don't understand how parse json to list or any types in flutter
https://jsonplaceholder.typicode.com/photos <= this is json example what i use
and that info is surrounded by [], {}
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
List<Photo> simple =
parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
i expect when json.decode.cast() work, parsed contain each objects but
when i print parsed, it's just one array like [~~~]
why they use cast method with Map<>?
jsonDecode already gives you the list object, so you can optimize the code.
In your case , instead of using
final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();
List<Photo> simple =
parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
try using
final List<Photo> simple = jsonDecode(responseBody).map((item) => Photo(title: item.title)).toList()
and you avoid having a fromJson function
You do not need to cast the array because they are already a list of objects.
You can use the following to get a list of photo objects:
Future<String> getPhotos() async {
var response = await http.get(
'https://jsonplaceholder.typicode.com/photos');
if (response.statusCode == 200) {
var parsed = json.decode(response.body);
List<Photo> simple = parsed.map<Photo>((json) => Photo.fromJson(json)).toList();
print(simple);
}
}
This is the photo class used.
class Photo {
final int albumId;
final int id;
final String title;
final String url;
final String thumbnailUrl;
Photo(
{this.albumId, this.id, this.title, this.url, this.thumbnailUrl});
factory Photo.fromJson(Map<dynamic, dynamic> json) {
return Photo(albumId: json['albumId'],
id: json['id'],
title: json['title'],
url: json['url'],
thumbnailUrl: json['thumbnailUrl'],
);
}
}

Getting type 'List<dynamic>' is not a subtype of type 'List<...>' error in JSON

I'm decoding a response body and I'm getting the error:
'List<dynamic>' is not a subtype of type 'List<Example>'
I'm parsing a JSON array of JSON objects, one of the fields is a list of objects as well and I suspect my issue stems from that. I am also using the json_serializable library. Below is my code, I omitted some fields out and changed some variable names but it represents the same code:
import 'package:json_annotation/json_annotation.dart';
part 'example_model.g.dart';
#JsonSerializable()
class Example {
(some fields here)
final List<Random> some_urls;
final List<String> file_urls;
const Example({
(some fields here)
this.some_urls,
this.file_urls,
});
factory Example.fromJson(Map<String, dynamic> json) =>
_$ ExampleFromJson(json);
}
#JsonSerializable()
class Random {
final String field_1;
final int field_2;
final int field_3;
final int field_4;
final bool field_5;
constRandom(
{this.field_1, this.field_2, this.field_3, this.field_4, this.field_5});
factory Random.fromJson(Map<String, dynamic> json) => _$RandomFromJson(json);
}
from the .g dart file that json_serializable made (ommited the encoding part):
Example _$ExampleFromJson(Map<String, dynamic> json) {
return Example(
some_urls: (json['some_urls'] as List)
?.map((e) =>
e == null ? null : Random.fromJson(e as Map<String, dynamic>))
?.toList(),
file_urls: (json['file_urls'] as List)?.map((e) => e as String)?.toList(),
}
Random _$RandomFromJson(Map<String, dynamic> json) {
return Random(
field_1: json['field_1'] as String,
field_2: json['field_2'] as int,
field_3: json['field_3'] as int,
field_4: json['field_4'] as int,
field_5: json['field_5'] as bool);
}
This is my future function:
Future<List<Example>> getData(int ID, String session) {
String userID = ID.toString();
var url = BASE_URL + ":8080/example?userid=${userID}";
return http.get(url, headers: {
"Cookie": "characters=${session}"
}).then((http.Response response) {
if (response.statusCode == 200) {
var parsed = json.decode(response.body);
List<Example> list = parsed.map((i) => Example.fromJson(i)).toList();
return list;
}
}).catchError((e)=>print(e));
}
This code creates a List<dynamic>
parsed.map((i) => Example.fromJson(i)).toList();
You must explicitly cast List<dynamic> to List<Example> like so,
List<Example> list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
or just
var /* or final */ list = List<Example>.from(parsed.map((i) => Example.fromJson(i)));
See also
In Dart, what's the difference between List.from and .of, and between Map.from and .of?
https://api.dartlang.org/stable/2.0.0/dart-core/List/List.from.html
https://api.dartlang.org/stable/2.0.0/dart-core/List/List.of.html
Dart 2.X List.cast() does not compose
Reason for Error:
You get this error when your source List is of type dynamic or Object (let's say) and you directly assign it to a specific type without casting.
List<dynamic> source = [1];
List<int> ints = source; // error
Solution:
You need to cast your List<dynamic> to List<int> (desired type), there are many ways of doing it. I am listing a few here:
List<int> ints = List<int>.from(source);
List<int> ints = List.castFrom<dynamic, int>(source);
List<int> ints = source.cast<int>();
List<int> ints = source.map((e) => e as int).toList();
I was receiving the 'MappedListIterable<dynamic, dynamic>' is not a subtype of type 'Iterable<Example> when i tried Günter's solution.
var parsed = json.decode(response.body);
var list = parsed.map((i) => Example.fromJson(i)).toList();
Casting the parsed data into a List<dynamic> (rather than just letting it go to dynamic) resolved that issue for me.
var parsed = json.decode(response.body) as List<dynamic>;
var list = parsed.map((i) => Example.fromJson(i)).toList();
Your code:
var parsed = json.decode(response.body);
List<Example> list = parsed.map((i) => Example.fromJson(i)).toList();
Can be replaced with this code:
import 'package:json_helpers/json_helpers.dart';
final examples = response.body.jsonList((e) => Example.fromJson(e));
And everything will work as you expected...