Getting errors after trying to convert nested JSON data using dart's factory concept.
Here I have two classes to handle the json data, but still getting this error:
Exception has occurred.
_TypeError (type 'FormatException' is not a subtype of type 'Map')
Here is the code:
class BodyResponse {
final Map<String, dynamic> message;
BodyResponse({
this.message
});
factory BodyResponse.fromJson(Map<String, dynamic> json) {
return BodyResponse(message: json['message']);
}
}
class ErrorResponse {
final BodyResponse body;
final int status;
final String contentType;
final bool success;
ErrorResponse({
this.body, this.status, this.contentType, this.success
});
factory ErrorResponse.fromJson(Map<String, dynamic> json) {
return ErrorResponse(
body: BodyResponse.fromJson(json['body']),
status: json['status'],
contentType: json['content_type'],
success: json['success']
);
}
}
ErrorResponse errors = ErrorResponse.fromJson("""
{
"body": {
"message": "Some one has already taken this username(fooBar), please try again with a new username."
},
"status": 500,
"content_type": "application\/json",
"success": false
}
""");
print(errors);
What could go wrong here?
Modified most of your code here. Hope that this is what you tried to achieve.
import 'dart:convert';
class BodyResponse {
final String message;
BodyResponse({this.message});
BodyResponse.fromJson(Map<String, dynamic> json):
message = json['message'];
factory BodyResponse.fromString(String encodedJson) {
return BodyResponse.fromJson(json.decode(encodedJson));
}
Map<String, dynamic> toJson() => {
"message": message,
};
String toString() => json.encode(this.toJson());
}
class ErrorResponse {
final BodyResponse body;
final int status;
final String contentType;
final bool success;
ErrorResponse({this.body, this.status, this.contentType, this.success});
ErrorResponse.fromJson(Map<String, dynamic> json):
body = BodyResponse.fromJson(json['body']),
status = json['status'],
contentType = json['content_type'],
success = json['success'];
factory ErrorResponse.fromString(String encodedJson) {
return ErrorResponse.fromJson(json.decode(encodedJson));
}
Map<String, dynamic> toJson() => {
"body": body.toJson(),
"status": status,
"contentType": contentType,
"success": success,
};
String toString() => json.encode(this.toJson());
}
void main() {
ErrorResponse errors = ErrorResponse.fromString("""
{
"body": {
"message": "Some one has already taken this username(fooBar), please try again with a new username."
},
"status": 500,
"content_type": "application\/json",
"success": false
}
""");
print(errors);
}
Let me know if this helped.
Related
Hi am trying to display the list of info from api i have created api and model and provider classes respectively when i try to access the api am getting this "List' is not a subtype of type 'Map<String, dynamic>"
where my api response is given below
[
{
"id": 1,
"username": "naveen",
"email": "demo#email.com",
"scheduledDate": "15-09-2022",
"remarks": "demo",
"createdAt": "2022-09-14T23:57:09.344Z",
"updatedAt": "2022-09-14T23:57:09.344Z"
},
{
"id": 2,
"username": "naveen",
"email": "demo#email.com",
"scheduledDate": "16-09-2022",
"remarks": "demo",
"createdAt": "2022-09-14T23:57:17.756Z",
"updatedAt": "2022-09-14T23:57:17.756Z"
}
]
and my model class
class ScheduleListModel {
int? id;
String? username;
String? email;
String? scheduledDate;
String? remarks;
String? createdAt;
String? updatedAt;
ScheduleListModel(
{this.id,
this.username,
this.email,
this.scheduledDate,
this.remarks,
this.createdAt,
this.updatedAt});
ScheduleListModel.fromJson(Map<String, dynamic> json) {
id = json['id'];
username = json['username'];
email = json['email'];
scheduledDate = json['scheduledDate'];
remarks = json['remarks'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['username'] = username;
data['email'] = email;
data['scheduledDate'] = scheduledDate;
data['remarks'] = remarks;
data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt;
return data;
}
}
and provider class
class ScheduleListProvider extends ChangeNotifier {
bool isBack = false;
late ScheduleListModel scheduleListModel;
ScheduleListModel get getScheduleListModel => scheduleListModel;
Future<void> getAllScheduleListData() async {
getAllSchedule().then((response) => {
if (response!.statusCode == 200)
{
scheduleListModel =
ScheduleListModel.fromJson(json.decode(response.body)),
notifyListeners(),
}
});
}
}
this is my api call
Future<http.Response?> getAllSchedule() async {
late SharedPreferences logindata;
logindata = await SharedPreferences.getInstance();
late String? token = logindata.getString('token');
http.Response? response;
try {
response = await http.get(Uri.parse(Config.getAllScheduleAPI), headers: {
HttpHeaders.contentTypeHeader: "application/json",
'x-access-token': '$token',
});
} catch (e) {
log(e.toString());
}
return response;
}
when am trying to do api call am getting exception as
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
help me to solve this
thank you very much in advance
Your api response is List<ScheduleListModel>, not a single ScheduleListModel. Thats why
ScheduleListModel.fromJson(json.decode(response.body)),
throws this error message.
Depending on what you want to do with the list, you should change getAllScheduleListData() to something which can work with a list of ScheduleListModel.
Try this:
class ScheduleListProvider extends ChangeNotifier {
bool isBack = false;
List<ScheduleListModel> scheduleList = [];
Future<void> getAllScheduleListData() async {
getAllSchedule().then((response) => {
if (response!.statusCode == 200) {
json.decode(response.body).forEach((element){
scheduleList.add(ScheduleListModel.fromJson(element));
});
notifyListeners(),
}
});
}
}
I am uploading images to a storageAPI using POST method with content-type of multipart/form-data. The api returns an object response that looks as below:
{
"id": "6d50c066-cf65-4748-8b9a-183c3526f49b",
"name": "hotel_6.jpg",
"fileKey": "lv/im/5d9feb8e-2ea8-439d-a550-1e937081e085-hotel_6.jpg",
"fileExtension": ".jpg",
"mimeType": "image/jpeg",
"catalogueUrl": {
"mainUrl": "https://xy.abc.com/lv/im/5d9feb8e-2ea8-439d-a550-1e937081e085-hotel_6.jpg",
"thumbnailUrls": []
},
"createdAt": "2021-11-25T06:40:40.0869466+00:00"
}
How can I extract the variable "mainUrl" from the response so that I can assign its value to the _pictureController? Here is what I have done:
uploadFile() async {
var accessToken = await sharedPref.read(key);
var postUrl = '$baseUrl/catalogue?thumbnail=${param.thumbnailTrueFalse}';
Map < String, String > headers = {
"Authorization": "Bearer $accessToken",
};
// multipart request object
var request = http.MultipartRequest("POST", Uri.parse(postUrl));
request.headers.addAll(headers);
// add selected file with request
request.files.add(http.MultipartFile("file", imageStream, imageSize,
filename: imageName));
// Send request
var response = await request.send();
// Read response
var result = await response.stream.bytesToString();
print('readResponse: $result');
if (response.statusCode == 200) {
var data = StorageResponse.fromJson(jsonDecode(result));
print('data: $data');
setState(() {
_pictureController.text = data.catalogueUrl!.mainUrl!;
});
return data;
} else {
throw Exception('Failed to upload photo.');
}
}
The "StorageResponse" Class is as follows:
#JsonSerializable()
class StorageResponse {
var id;
var name;
var fileKey;
var fileExtension;
var mimeType;
Catalogue ? catalogueUrl;
var createdAt;
StorageResponse({
this.id,
this.name,
this.fileKey,
this.fileExtension,
this.mimeType,
this.catalogueUrl,
this.createdAt,
});
factory StorageResponse.fromJson(Map < String, dynamic > json) =>
_$StorageResponseFromJson(json);
Map < String, dynamic > toJson() => _$StorageResponseToJson(this);
#override
toString() {
String output =
'{id:${this.id},name:${this.name},fileKey: ${this.fileKey},fileExtension:${this.fileExtension},mimeType: ${this.mimeType}mimeType},catalogueUrl: ${this.catalogueUrl},,createdAt: ${this.createdAt}}';
return output;
}
}
You can use the following structure to convert a Json file to a class, and vice versa.
The following structure works properly.
import 'dart:convert';
class StorageResponse {
final String id;
final String name;
final String fileKey;
final String fileExtension;
final String mimeType;
Catalogue catalogueUrl;
final DateTime createdAt;
StorageResponse(
this.id,
this.name,
this.fileKey,
this.fileExtension,
this.mimeType,
this.catalogueUrl,
this.createdAt,
);
factory StorageResponse.fromMap(Map<String, dynamic> json) {
return StorageResponse(
json['id'],
json['name'],
json['fileKey'],
json['fileExtension'],
json['mimeType'],
Catalogue.fromMap(json['Catalogue']),
DateTime.parse(json['createdAt']));
}
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'fileKey': fileKey,
'fileExtension': fileExtension,
'mimeType': mimeType,
'Catalogue': catalogueUrl.toJson(),
'createdAt': createdAt
};
#override
toString() {
String output =
'{id:${this.id},name:${this.name},fileKey: ${this.fileKey},fileExtension:${this.fileExtension},mimeType: ${this.mimeType}mimeType},catalogueUrl: ${this.catalogueUrl},,createdAt: ${this.createdAt}}';
return output;
}
}
class Catalogue {
final String mainUrl;
final List<String> thumbnailUrls;
Catalogue(this.mainUrl, this.thumbnailUrls);
factory Catalogue.fromMap(Map<String, dynamic> json) {
return Catalogue(json['mainUrl'], jsonDecode(json['thumbnailUrls']));
}
Map<String, dynamic> toJson() =>
{'mainUrl': mainUrl, 'thumbnailUrls': jsonEncode(thumbnailUrls)};
}
for use
if (response.statusCode == 200) {
var data = StorageResponse.fromMap(jsonDecode(result));
print('data: $data');
setState(() {
_pictureController.text = data.catalogueUrl!.mainUrl!;
});
return data;
} else {
throw Exception('Failed to upload photo.');
}
I have a flutter application, which uses some server APIs.
When there is a successful response from the server, it would return json back:
{"success": true, "message": "success", "data": {"id": "21EE"} }
However, when there is failure, it would return:
{"success": false, "message": "failure"}
For more strict-typed use of flutter, I try to model the response.
Here is my try:
class ServerResponse {
final bool success;
final String message;
ServerResponse({
required this.success,
required this.message,
});
}
class _AuthAutenticationData {
final String id;
_AuthAutenticationData({
required this.id,
});
}
class AutoAuthenticateResponse extends ServerResponse {
final _AuthAutenticationData? data;
AutoAuthenticateResponse({
required success,
required message,
this.data,
}) : super(success: success, message: message);
}
Now, I have a function which calls a specific API:
Future<void> autoAuth() async {
final url = Uri.parse('${this._baseURL.toString()}/auto-auth');
try {
final response = await http.post(url, headers: {
'Authorization': 'SXXX',
});
print(response.body);
final AutoAuthenticateResponse responseBody = json.decode(response.body);
if (responseBody.success) {
return setUser(new User(id: responseBody.data!.id));
}
setUser(null);
} catch (error) {
print(error);
setUser(null);
}
}
Some code is irrelevant, but bottom line is that I receive the following error in the line: final AutoAuthenticateResponse responseBody = json.decode(response.body);:
I/flutter (14139): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'AutoAuthenticateResponse'
I guess my solution is bad. Any advice how to fix it?
Well, you can use nullsafety feature for this. Since it's only if when the failure the data is not being returned.
{"success": true, "message": "success", "data": {"id": "21EE"} }
you can use this :
https://meruya-techhnology.github.io/json-to-dart/
Then the result will be a 2 class
class YourClass {
final bool success;
final String message;
final Data? data;
YourClass({this.success, this.message, this.data});
factory YourClass.fromJson(Map<String, dynamic> json) => YourClass(
success : json['success'],
message : json['message'],
data : json['data'] != null ? new Data.fromJson(json['data']) : null;
);
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['success'] = this.success;
data['message'] = this.message;
if (this.data != null) {
data['data'] = this.data.toJson();
}
return data;
}
}
And
class Data {
String id;
Data({this.id});
Data.fromJson(Map<String, dynamic> json) {
id = json['id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
return data;
}
}
Then you can use it like this :
Future<void> autoAuth() async {
final url = Uri.parse('${this._baseURL.toString()}/auto-auth');
try {
final response = await http.post(url, headers: {
'Authorization': 'SXXX',
});
debugPrint(response.body);
final responseBody = YourClass.fromJson(response.body)
if (responseBody.success) {
return setUser(new User(id: responseBody.data!.id));
}
setUser(null);
} catch (error) {
print(error);
setUser(null);
}
}
another advice is use debugPrint instead of print, here you can read more : https://medium.com/flutter-community/debugprint-and-the-power-of-hiding-and-customizing-your-logs-in-dart-86881df05929
Use this link to generate models for your response
I am fetching some data from an API, which returns a Json array, promotions_model.dart does all the parsing, but this error is showing up.
Error--
A value of type 'Result' can't be returned from function 'fetchPromotions' because it has a return type of 'Future<List>'.
can someone please tell me what i am doing wrong here. thanks
**promotions_model.dart**
import 'dart:convert';
Result resultFromJson(String str) => Result.fromJson(json.decode(str));
String resultToJson(Result data) => json.encode(data.toJson());
class Result {
Result({
this.code,
this.result,
});
final int code;
final List<Promotions> result;
factory Result.fromJson(Map<String, dynamic> json) => Result(
code: json["Code"],
result: List<Promotions>.from(
json["Result"].map((x) => Promotions.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"Code": code,
"Result": List<dynamic>.from(result.map((x) => x.toJson())),
};
}
class Promotions {
Promotions({
this.id,
this.title,
this.description,
this.image,
});
final String id;
final String title;
final String description;
final String image;
factory Promotions.fromJson(Map<String, dynamic> json) => Promotions(
id: json["id"],
title: json["title"],
description: json["description"],
image: json["image"],
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"description": description,
"image": image,
};
}
**promotion-api.dart**
import 'dart:async';
import 'package:http/http.dart' as http;
import 'package:project/models/promotions_model.dart';
const key = {
'APP-X-RESTAPI-KEY': "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
};
const API = 'http://111.111.11.1/project';
Future<List<Promotions>> fetchPromotions() async {
final response = await http.get(API + '/promotion/all', headers: key);
if (response.statusCode == 200) {
return resultFromJson(response.body); // This line is causing the error
} else {
print(response.statusCode);
}
}
The Error says it clearly. It needs Result as the return type.
You can something like this,
Result fetchPromotions() async {
final response = await http.get(API + '/promotion/all', headers: key);
Result result = null;
if (response.statusCode == 200) {
result = resultFromJson(response.body); // This line is causing the error
} else {
print(response.statusCode);
}
return result;
}
Hope you got an idea.
return resultFromJson(response.body);
This line returns a Result, not a List<Promotion>.
In my Flutter app I receive notifications with a custom payload, like:
{ notification:
{
title: Test,
body: AAAA
},
data:
{
productId: Axe,
page: Products,
click_action: FLUTTER_NOTIFICATION_CLICK
}
}
Everything works well. I also can handle the payload of the notification by access it through:
message['data']['page']
But I rather would like to use an Interface/Class to name the data by key, for example:
message.data.page and message.data.productId
So I tried:
class NotificationMessage {
Map notification;
Map data;
NotificationMessage(this.notification, this.data);
}
...
NotificationMessage _message = message; // Assigning the payload to the final
...
This where I get stuck: here I got the error: A value of type 'Map<dynamic, dynamic>' can't be assigned to a variable of type 'NotificationMessage'.
My class isn't finished yet, but how to continue?
I' aware of json_serializable, but before any tooling, I would like to understand it fully.
First, you need to build the two models for notification and data as follows
class DataMessage {
final String productId;
final String page;
final String click_action;
DataMessage(this.productId, this.page, this.click_action);
factory DataMessage.fromJson(Map<dynamic, dynamic> json) {
return DataMessage(
json['productId'] as String,
json['page'] as String,
json['click_action'] as String,
);
}
}
class NotificationMessage {
final String title;
final String body;
NotificationMessage(this.title, this.body);
factory NotificationMessage.fromJson(Map<dynamic, dynamic> json) {
return NotificationMessage(
json['title'] as String,
json['body'] as String,
);
}
}
The factory method convert map types into model classes.
Then you have to build a model for the response message as follows
class Message {
final NotificationMessage notification;
final DataMessage data;
Message(this.notification, this.data);
factory Message.fromJson(Map<dynamic, dynamic> json) {
final Map<dynamic, dynamic> mapNotification = json['notification'];
final Map<dynamic, dynamic> mapData = json['data'];
final dataModel = DataMessage.fromJson(mapData);
final notificationModel = NotificationMessage.fromJson(mapNotification);
return Message(
notificationModel as NotificationMessage,
dataModel as DataMessage,
);
}
}
Note that the factory method allows you to convert the maps for each model to a class model
So you can define your response as a class
Map<dynamic, dynamic> messageResponse = {
'notification': {'title': 'Test', 'body': 'AAAA'},
'data': {
'productId': 'Axe',
'page': 'Products',
'click_action': 'FLUTTER_NOTIFICATION_CLICK'
}
};
final Message message = Message.fromJson(messageResponse);
print(message.data.productId);
print(message.data.page);
print(message.data.click_action);
print(message.notification.title);
print(message.notification.body);
Hope that can help you
Instance of the json object. this way you can use a map for any instance without modifying the class
{ "notification":
{
"title": "Test",
"body": "AAAA"
},
"data":
{
"productId": "Axe",
"page": "Products",
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}
Class looks like;
class NotificationMessage {
NotificationMessage({
this.notification,
this.data,
});
final Notification notification;
final Data data;
factory NotificationMessage.fromJson(Map<String, dynamic> json) => NotificationMessage(
notification: Notification.fromJson(json["notification"]),
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"notification": notification.toJson(),
"data": data.toJson(),
};
}
class Data {
Data({
this.productId,
this.page,
this.clickAction,
});
final String productId;
final String page;
final String clickAction;
factory Data.fromJson(Map<String, dynamic> json) => Data(
productId: json["productId"],
page: json["page"],
clickAction: json["click_action"],
);
Map<String, dynamic> toJson() => {
"productId": productId,
"page": page,
"click_action": clickAction,
};
}
class Notification {
Notification({
this.title,
this.body,
});
final String title;
final String body;
factory Notification.fromJson(Map<String, dynamic> json) => Notification(
title: json["title"],
body: json["body"],
);
Map<String, dynamic> toJson() => {
"title": title,
"body": body,
};
}
A function to build as
Future<NotificationMessage> getNotf() {
var parsedJson = json.decode(//your received object);
return NotificationMessage.fromJson(parsedJson);
}
Now you can receive this in a builder such as
_futureNotifications =Future<NotificationMessage>
_futureNotifications = getNotf(); //this is a function and can be used after a gesture or initState
FutureBuilder<NotificationMessage>(
future: _futureNotifications,
builder: (context, snapshot) {
}