'Flutter Convert Future<object> to Map<String, dynamic> [duplicate]
I need to make HTTP Post JSON request but first i need to Get 3 different JSONs and make 1 of them. I thought better way to do this is to work with Map objects to combine them and then Post. Now i want to know how to convert Future to Map<String, dynamic>
This is the main.dart to read the json:
import 'dart:convert';
import 'package:api/models/processing.dart';
import 'package:http/http.dart' as http;
main() async {
fetchProcessing();
}
Future<Processing> fetchProcessing() async {
final response = await http.get(
Uri.parse(
'url'),
headers: {
'authorization': 'Bearer token',
'Content-Type': 'application/json',
"Access-Control_Allow_Origin": "*",
});
if (response.statusCode == 200) {
return Processing.fromJson(response.body);
} else {
print(response.statusCode);
throw Exception('Failed to load JSON');
}
}
Here is Model class for JSON:
import 'dart:convert';
class Processing {
Processing({
required this.organization,
required this.processingSum,
required this.quantity,
required this.processingPlan,
required this.productsStore,
required this.materialsStore,
required this.products,
required this.materials,
});
final MaterialsStore organization;
final double processingSum;
final double quantity;
final MaterialsStore processingPlan;
final MaterialsStore productsStore;
final MaterialsStore materialsStore;
final Materials products;
final Materials materials;
factory Processing.fromJson(Map<String, dynamic> json) => Processing(
organization: MaterialsStore.fromJson(json["organization"]),
processingSum: json["processingSum"],
quantity: json["quantity"],
processingPlan: MaterialsStore.fromJson(json["processingPlan"]),
productsStore: MaterialsStore.fromJson(json["productsStore"]),
materialsStore: MaterialsStore.fromJson(json["materialsStore"]),
products: Materials.fromJson(json["products"]),
materials: Materials.fromJson(json["materials"]),
);
Map<String, dynamic> toJson() => {
"organization": organization.toJson(),
"processingSum": processingSum,
"quantity": quantity,
"processingPlan": processingPlan.toJson(),
"productsStore": productsStore.toJson(),
"materialsStore": materialsStore.toJson(),
"products": products.toJson(),
"materials": materials.toJson(),
};
}
class Materials {
Materials();
factory Materials.fromJson(Map<String, dynamic> json) => Materials();
Map<String, dynamic> toJson() => {};
}
class MaterialsStore {
MaterialsStore({
required this.meta,
});
final Meta meta;
factory MaterialsStore.fromJson(Map<String, dynamic> json) => MaterialsStore(
meta: Meta.fromJson(json["meta"]),
);
Map<String, dynamic> toJson() => {
"meta": meta.toJson(),
};
}
class Meta {
Meta({
required this.href,
required this.metadataHref,
required this.type,
required this.mediaType,
required this.uuidHref,
});
final String href;
final String metadataHref;
final String type;
final String mediaType;
final String uuidHref;
factory Meta.fromJson(Map<String, dynamic> json) => Meta(
href: json["href"],
metadataHref: json["metadataHref"],
type: json["type"],
mediaType: json["mediaType"],
uuidHref: json["uuidHref"],
);
Map<String, dynamic> toJson() => {
"href": href,
"metadataHref": metadataHref,
"type": type,
"mediaType": mediaType,
"uuidHref": uuidHref,
};
}
Here is JSON i'm parsing:
{
"organization" : {
"meta" : {
"href" : "https://online.moysklad.ru/api/remap/1.2/entity/organization/5a0f5b7b-a5a7-11ea-0a80-03a2000bb129",
"metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/organization/metadata",
"type" : "organization",
"mediaType" : "application/json",
"uuidHref" : "https://online.moysklad.ru/app/#mycompany/edit?id=5a0f5b7b-a5a7-11ea-0a80-03a2000bb129"
}
},
"processingSum" : 10000.0,
"quantity" : 3.0,
"processingPlan" : {
"meta" : {
"href" : "https://online.moysklad.ru/api/remap/1.2/entity/processingplan/2b0530e5-57e2-11ec-0a80-027e001adc60",
"metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/processingplan/metadata",
"type" : "processingplan",
"mediaType" : "application/json",
"uuidHref" : "https://online.moysklad.ru/app/#processingplan/edit?id=2b0530e5-57e2-11ec-0a80-027e001adc60"
}
},
"productsStore" : {
"meta" : {
"href" : "https://online.moysklad.ru/api/remap/1.2/entity/store/5a10ca61-a5a7-11ea-0a80-03a2000bb12b",
"metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/store/metadata",
"type" : "store",
"mediaType" : "application/json",
"uuidHref" : "https://online.moysklad.ru/app/#warehouse/edit?id=5a10ca61-a5a7-11ea-0a80-03a2000bb12b"
}
},
"materialsStore" : {
"meta" : {
"href" : "https://online.moysklad.ru/api/remap/1.2/entity/store/5a10ca61-a5a7-11ea-0a80-03a2000bb12b",
"metadataHref" : "https://online.moysklad.ru/api/remap/1.2/entity/store/metadata",
"type" : "store",
"mediaType" : "application/json",
"uuidHref" : "https://online.moysklad.ru/app/#warehouse/edit?id=5a10ca61-a5a7-11ea-0a80-03a2000bb12b"
}
},
"products" : {},
"materials" : {}
}
Solution 1:[1]
Use then()
or whenComplete()
. Example:
fetchProcessing().then((value) {
// your code
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |