class AllRideRequest {
String? name;
String? destinationAddress;
DriverLocation? driverLocation;
AllRideRequest({
this.name,
this.destinationAddress, this.driverLocation
});
AllRideRequest.fromSnapshot(DataSnapshot dataSnapshot) {
name = (dataSnapshot.value as Map)["userName"];
destinationAddress = (dataSnapshot.value as Map)["destinationAddress"];
driverLocation = DriverLocation.fromSnapshot(dataSnapshot);
}
}
class DriverLocation {
double? latitude;
double? longitude;
DriverLocation({
this.latitude,
this.longitude,
});
DriverLocation.fromSnapshot(DataSnapshot dataSnapshot) {
latitude = (dataSnapshot.value as Map)["latitude"];
longitude = (dataSnapshot.value as Map)["longitude"];
}
}
i have a data model like this. Name and destinationAddress has a value but DriverLocation lat long null how can i fix this problem
i want to reach this
https://i.stack.imgur.com/WsoOF.png
Check this
class ResponseData {
String? userName;
String? destinationAddress;
DriverLocation? driverLocation;
ResponseData({this.userName, this.destinationAddress, this.driverLocation});
ResponseData.fromJson(Map<String, dynamic> json) {
userName = json['userName'];
destinationAddress = json['destinationAddress'];
driverLocation = json['driverLocation'] != null
? new DriverLocation.fromJson(json['driverLocation'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['userName'] = this.userName;
data['destinationAddress'] = this.destinationAddress;
if (this.driverLocation != null) {
data['driverLocation'] = this.driverLocation!.toJson();
}
return data;
}
}
class DriverLocation {
String? latitude;
String? longitude;
DriverLocation({this.latitude, this.longitude});
DriverLocation.fromJson(Map<String, dynamic> json) {
latitude = json['latitude'];
longitude = json['longitude'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['latitude'] = this.latitude;
data['longitude'] = this.longitude;
return data;
}
}
Related
How do I retrieve the information in the address? Attempted to retrieve information I can fetch but the Items class part is not fetching the address part. I'm practicing the fetch api.
I'm not sure if what I'm doing is correct. or may be stuck with some part of the problem i try to fix please help me
List<Items> _list = [];
List<Items> _search = [];
var loading = false;
Future fetchMos() async {
setState(() {
loading = true;
});
_list.clear();
var client = http.Client();
String mosUrl =
'';
var url = Uri.parse(mosUrl);
var headers = {'Client-Token': ''};
var response = await client.get(url, headers: headers);
if (response.statusCode == 200) {
var data = jsonDecode((utf8.decode(response.bodyBytes)))['items'];
setState(() {
for (Map i in data) {
_list.add(Items.fromJson(i));
loading = false;
}
});
}
}
This is class model
class Items {
String? custnum;
String? name;
List<Address>? address;
Items({this.custnum, this.name, this.address});
Items.fromJson(json) {
custnum = json['custnum'];
name = json['name'];
if (json['address'] != null) {
address = <Address>[];
json['address'].forEach((v) {
address!.add(new Address.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['custnum'] = this.custnum;
data['name'] = this.name;
if (this.address != null) {
data['address'] = this.address!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Address {
int? shipto;
String? addr1;
String? thanon;
String? tambon;
String? amphur;
String? provCode;
String? province;
String? country;
String? phone;
String? email;
String? postcode;
String? contact;
String? latitude;
String? longitude;
String? fax;
String? soi;
Address(
{this.shipto,
this.addr1,
this.thanon,
this.tambon,
this.amphur,
this.provCode,
this.province,
this.zipcode,
this.country,
this.phone,
this.email,
this.postcode,
this.contact,
this.latitude,
this.longitude,
this.fax,
this.soi});
Address.fromJson(json) {
shipto = json['shipto'];
addr1 = json['addr1'];
thanon = json['thanon'];
tambon = json['tambon'];
amphur = json['amphur'];
provCode = json['prov_code'];
province = json['province'];
zipcode = json['zipcode'];
country = json['country'];
phone = json['phone'];
email = json['email'];
postcode = json['postcode'];
contact = json['contact'];
latitude = json['latitude'];
longitude = json['longitude'];
fax = json['fax'];
soi = json['soi'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['shipto'] = this.shipto;
data['addr1'] = this.addr1;
data['thanon'] = this.thanon;
data['tambon'] = this.tambon;
data['amphur'] = this.amphur;
data['prov_code'] = this.provCode;
data['province'] = this.province;
data['zipcode'] = this.zipcode;
data['phone'] = this.phone;
data['email'] = this.email;
data['postcode'] = this.postcode;
data['contact'] = this.contact;
data['longitude'] = this.longitude;
data['fax'] = this.fax;
data['soi'] = this.soi;
return data;
}
}
var data =json.decode(response.body);
for (var i in data['items']) {
_list.add(Items.fromJson(i));
loading = false;
}
setState(() { });
I'm trying to save a list of object into a shared preference for easy storage. I'm getting the error [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: FormatException: Unexpected character (at character 1) -> E/flutter ( 7980): Instance of 'History'
Method
Future<void> recordLocation(lat, long) async {
List<History> histories = [];
final prefs = await SharedPreferences.getInstance();
//fetching history
if (prefs.getStringList('history') != null) {
List<String>? prevHistory = prefs.getStringList('history');
histories = prevHistory!
.map<History>((i) => History.fromJson(json.decode(i)))
.toList();
}
//setting history
History current = History(lat.toString(), long.toString());
histories.add(current);
List<String> newHistory = histories.map((i) => i.toString()).toList();
await prefs.setStringList('history', newHistory);
}
History model
class History {
String? latitude;
String? longtitude;
History(this.latitude, this.longtitude);
History.fromJson(Map<String, dynamic> json) {
latitude = json['latitude'];
longtitude = json['longtitude'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['latitude'] = this.latitude;
data['longtitude'] = this.longtitude;
return data;
}
}
People usually declare the fromJson method as a factory method and data classes as immutable. Try this instead:
class History {
final String latitude;
final String longtitude;
const History({
this.latitude = '',
this.longtitude = '',
});
factory History.fromJson(Map<String, dynamic> json) =>
History(
latitude: json['latitude'] as String? ?? '',
longtitude: json['longtitude'] as String? ?? '',
);
Map<String, dynamic> toJson() => {
'latitude': latitude,
'longtitude': longtitude,
};
}
Edit:
import 'dart:convert';
class History {
String? latitude;
String? longtitude;
History(this.latitude, this.longtitude);
History.fromJson(Map<String, dynamic> json) {
latitude = json['latitude'].toString();
longtitude = json['longtitude'].toString();
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['latitude'] = this.latitude;
data['longtitude'] = this.longtitude;
return data;
}
#override
String toString() {
String res = '{"latitude":${latitude ?? ""},"longtitude":${longtitude ?? ""}}';
return res;
}
}
void main(){
List<History> histories = [];
List<String>? prevHistory = [];
for(int i=0;i<10;i++){
History his = History(i.toString(), i.toString());
prevHistory!.add(his.toString());
}
if(prevHistory != null){
histories = prevHistory!.map<History>((i){
return History.fromJson(jsonDecode(i));
}).toList();
}
for(int i=0;i<histories.length;i++){
print(histories[i].toString());
}
}
I have run this code in dartpad and it gives the output as:
{"latitude":0,"longtitude":0}
{"latitude":1,"longtitude":1}
{"latitude":2,"longtitude":2}
{"latitude":3,"longtitude":3}
{"latitude":4,"longtitude":4}
{"latitude":5,"longtitude":5}
{"latitude":6,"longtitude":6}
{"latitude":7,"longtitude":7}
{"latitude":8,"longtitude":8}
{"latitude":9,"longtitude":9}
The problem was with the conversion of string to Map<String, dynamic>
Edit 2:
Here if you do not want to change your history class then just make edit in the toString() method:
String toString() {
String res = '{"latitude":"${latitude ?? ""}","longtitude":"${longtitude ?? ""}"}';
return res;
}
This is my model class and I am trying to get all the data but getting error and don't know why.
HomePageModel homePageModelFromJson(String str) => HomePageModel.fromJson(json.decode(str));
String homePageModelToJson(HomePageModel data) => json.encode(data.toJson());
class HomePageModel with ChangeNotifier {
HomePageModel({
this.data,
});
List<Datum>? data;
factory HomePageModel.fromJson(Map<String, dynamic> json) => HomePageModel(
data: List<Datum>.from(json["data"]!.map((x) => Datum.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"data": List<dynamic>.from(data!.map((x) => x.toJson())),
};
}
class Datum {
Datum({
this.schoolid,
this.name,
this.logo,
this.address,
this.contact,
this.principalname,
this.principalcontact,
this.slogan,
this.webAddress,
this.description,
this.email,
this.pan,
this.establishedYear,
});
String? schoolid;
String? name;
String? logo;
String? address;
String? contact;
String? principalname;
String? principalcontact;
String? slogan;
String? webAddress;
String? description;
String? email;
String? pan;
int? establishedYear;
factory Datum.fromJson(Map<String, dynamic> json) => Datum(
schoolid: json["schoolid"],
name: json["name"],
logo: json["logo"],
address: json["address"],
contact: json["contact"],
principalname: json["principalname"],
principalcontact: json["principalcontact"],
slogan: json["slogan"],
webAddress: json["web_address"] == null ? null : json["web_address"],
description: json["description"] == null ? null : json["description"],
email: json["email"],
pan: json["pan"],
establishedYear: json["established_year"],
);
Map<String, dynamic> toJson() => {
"schoolid": schoolid,
"name": name,
"logo": logo,
"address": address,
"contact": contact,
"principalname": principalname,
"principalcontact": principalcontact,
"slogan": slogan,
"web_address": webAddress == null ? null : webAddress,
"description": description == null ? null : description,
"email": email,
"pan": pan,
"established_year": establishedYear,
};
}
This is how I am trying to fetch data:
class HomePageModels with ChangeNotifier{
List<HomePageModel> _hItem = [];
List<HomePageModel> get hItem{
return [..._hItem];
}
Future<void> getHomeData(BuildContext context) async{
const url = "https://shikshyasoftware.com.np/CoreApplicationandAPIService-4617993073/api/school";
try{
// EasyLoading.show(status: 'Loading...');
final response = await http.get(Uri.parse(url));
final extractedData = json.decode(response.body);
List<HomePageModel> loadedHomeData = [];
if(extractedData == null){
return;
}
if(response.statusCode == 200){
print(extractedData);
}
extractedData.forEach((element){
loadedHomeData.add(HomePageModel.fromJson(element));
});
_hItem = loadedHomeData;
// EasyLoading.showSuccess("data fetched sucessfull");
notifyListeners();
}catch(e){
rethrow;
}
}
}
But I am getting error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type '(dynamic) => Null' is not a subtype of type '(String, dynamic) => void' of 'f'
The problem is the way you are trying to parse the data, you don't need to loop over every element to parse it, in your model just make it return a list type like this,
class HomePageModel with ChangeNotifier {
List<Datum>? data;
HomePageModel({this.data});
HomePageModel.fromJson(Map<String, dynamic> json) {
if (json['data'] != null) {
data = <Datum>[];
json['data'].forEach((v) {
data!.add(new Datum.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Datum {
Datum({
this.schoolid,
this.name,
this.logo,
this.address,
this.contact,
this.principalname,
this.principalcontact,
this.slogan,
this.webAddress,
this.description,
this.email,
this.pan,
this.establishedYear,
});
String? schoolid;
String? name;
String? logo;
String? address;
String? contact;
String? principalname;
String? principalcontact;
String? slogan;
String? webAddress;
String? description;
String? email;
String? pan;
int? establishedYear;
Datum.fromJson(Map<String, dynamic> json) {
schoolid = json["schoolid"];
name = json["name"];
logo = json["logo"];
address = json["address"];
contact = json["contact"];
principalname = json["principalname"];
principalcontact = json["principalcontact"];
slogan = json["slogan"];
webAddress = json["web_address"];
description = json["description"];
email = json["email"];
pan = json["pan"];
establishedYear = json["established_year"];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['schoolid'] = this.schoolid;
data['name'] = this.name;
data['logo'] = this.logo;
data['address'] = this.address;
data['contact'] = this.contact;
data['principalname'] = this.principalname;
data['principalcontact'] = this.principalcontact;
data['slogan'] = this.slogan;
data['web_address'] = this.webAddress;
data['description'] = this.description;
data['email'] = this.email;
data['pan'] = this.pan;
data['established_year'] = this.establishedYear;
return data;
}
}
and in your view model you can just parse the extracted data from response.body like this,
class HomePageModels with ChangeNotifier {
HomePageModel? _hItem;
HomePageModel get hItem {
return _hItem!;
}
Future<void> getHomeData(BuildContext context) async {
const url =
"https://shikshyasoftware.com.np/CoreApplicationandAPIService-
4617993073/api/school";
try {
// EasyLoading.show(status: 'Loading...');
final response = await http.get(Uri.parse(url));
final extractedData = json.decode(response.body);
if (extractedData == null) {
return;
}
if (response.statusCode == 200) {
print(extractedData);
}
HomePageModel loadedHomeData =
HomePageModel.fromJson(extractedData);
_hItem = loadedHomeData;
// EasyLoading.showSuccess("data fetched sucessfull");
notifyListeners();
} catch (e) {
rethrow;
}
}
}
getHomeData(BuildContext context) async {
const url =
"https://shikshyasoftware.com.np/CoreApplicationandAPIService-4617993073/api/school";
try {
// EasyLoading.show(status: 'Loading...');
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final extractedData = json.decode(response.body);
List loadedHomeData = extractedData;
_hItem = loadedHomeData.map((e) => HomePageModel.fromJson(e)).toList();
}
notifyListeners();
return _hItem;
} catch (e) {
rethrow;
}
}
Future<bool> login({username, password}) async {
var api = API();
_status = LoginStatus.loading;
notifyListeners();
var url = Uri.parse(api.baseUrl + api.auth);
final response = await http.post(
url,
body: jsonEncode({
"identifier": "$username",
"password": "$password",
}),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
final parsed = jsonDecode(response.body).cast<Map<String, dynamic>>();
parsed
.map<UserModel>((json) => UserModel.fromJson(json))
.toList();
final token = jsonDecode(response.body)['jwt'];
print(token);
await saveToken(token);
return true;
} else {
_status = LoginStatus.error;
_error = response.body;
notifyListeners();
return false;
}
}
Code Screen Shot
How Should I save this parsed JSON to UserModel? I have encountered many problems and figured out many things on my own but I am not yet able to add data to the model.
By the way I am using strapi as a back end and every api is working. And I amso use a website called json to dart converter so that my models are correct(As I Assume).
Please help !!!!!!!!!!!!
UserModel
class UserModel {
User user;
UserModel({this.user});
UserModel.fromJson(Map<String, dynamic> json) {
user = json['user'] != null ? new User.fromJson(json['user']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.user != null) {
data['user'] = this.user.toJson();
}
return data;
}
}
class User {
int id;
String username;
String email;
String provider;
bool confirmed;
bool blocked;
Role role;
String displayName;
String createdAt;
String updatedAt;
Avatar avatar;
List<UserCategories> userCategories;
User(
{this.id,
this.username,
this.email,
this.provider,
this.confirmed,
this.blocked,
this.role,
this.displayName,
this.createdAt,
this.updatedAt,
this.avatar,
this.userCategories});
User.fromJson(Map<String, dynamic> json) {
id = json['id'];
username = json['username'];
email = json['email'];
provider = json['provider'];
confirmed = json['confirmed'];
blocked = json['blocked'];
role = json['role'] != null ? new Role.fromJson(json['role']) : null;
displayName = json['displayName'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
avatar =
json['avatar'] != null ? new Avatar.fromJson(json['avatar']) : null;
if (json['user_categories'] != null) {
userCategories = new List<UserCategories>();
json['user_categories'].forEach((v) {
userCategories.add(new UserCategories.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['username'] = this.username;
data['email'] = this.email;
data['provider'] = this.provider;
data['confirmed'] = this.confirmed;
data['blocked'] = this.blocked;
if (this.role != null) {
data['role'] = this.role.toJson();
}
data['displayName'] = this.displayName;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
if (this.avatar != null) {
data['avatar'] = this.avatar.toJson();
}
if (this.userCategories != null) {
data['user_categories'] =
this.userCategories.map((v) => v.toJson()).toList();
}
return data;
}
}
class Role {
int id;
String name;
String description;
String type;
Role({this.id, this.name, this.description, this.type});
Role.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
description = json['description'];
type = json['type'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['description'] = this.description;
data['type'] = this.type;
return data;
}
}
class Avatar {
int id;
String name;
String alternativeText;
String caption;
int width;
int height;
Formats formats;
String hash;
String ext;
String mime;
double size;
String url;
Null previewUrl;
String provider;
Null providerMetadata;
String createdAt;
String updatedAt;
Avatar(
{this.id,
this.name,
this.alternativeText,
this.caption,
this.width,
this.height,
this.formats,
this.hash,
this.ext,
this.mime,
this.size,
this.url,
this.previewUrl,
this.provider,
this.providerMetadata,
this.createdAt,
this.updatedAt});
Avatar.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
alternativeText = json['alternativeText'];
caption = json['caption'];
width = json['width'];
height = json['height'];
formats =
json['formats'] != null ? new Formats.fromJson(json['formats']) : null;
hash = json['hash'];
ext = json['ext'];
mime = json['mime'];
size = json['size'];
url = json['url'];
previewUrl = json['previewUrl'];
provider = json['provider'];
providerMetadata = json['provider_metadata'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['alternativeText'] = this.alternativeText;
data['caption'] = this.caption;
data['width'] = this.width;
data['height'] = this.height;
if (this.formats != null) {
data['formats'] = this.formats.toJson();
}
data['hash'] = this.hash;
data['ext'] = this.ext;
data['mime'] = this.mime;
data['size'] = this.size;
data['url'] = this.url;
data['previewUrl'] = this.previewUrl;
data['provider'] = this.provider;
data['provider_metadata'] = this.providerMetadata;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
return data;
}
}
class Formats {
Thumbnail thumbnail;
Formats({this.thumbnail});
Formats.fromJson(Map<String, dynamic> json) {
thumbnail = json['thumbnail'] != null
? new Thumbnail.fromJson(json['thumbnail'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.thumbnail != null) {
data['thumbnail'] = this.thumbnail.toJson();
}
return data;
}
}
class Thumbnail {
String name;
String hash;
String ext;
String mime;
int width;
int height;
double size;
Null path;
String url;
Thumbnail(
{this.name,
this.hash,
this.ext,
this.mime,
this.width,
this.height,
this.size,
this.path,
this.url});
Thumbnail.fromJson(Map<String, dynamic> json) {
name = json['name'];
hash = json['hash'];
ext = json['ext'];
mime = json['mime'];
width = json['width'];
height = json['height'];
size = json['size'];
path = json['path'];
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['name'] = this.name;
data['hash'] = this.hash;
data['ext'] = this.ext;
data['mime'] = this.mime;
data['width'] = this.width;
data['height'] = this.height;
data['size'] = this.size;
data['path'] = this.path;
data['url'] = this.url;
return data;
}
}
class UserCategories {
int id;
String categoryName;
int usersPermissionsUser;
String publishedAt;
String createdAt;
String updatedAt;
UserCategories(
{this.id,
this.categoryName,
this.usersPermissionsUser,
this.publishedAt,
this.createdAt,
this.updatedAt});
UserCategories.fromJson(Map<String, dynamic> json) {
id = json['id'];
categoryName = json['categoryName'];
usersPermissionsUser = json['users_permissions_user'];
publishedAt = json['published_at'];
createdAt = json['created_at'];
updatedAt = json['updated_at'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['categoryName'] = this.categoryName;
data['users_permissions_user'] = this.usersPermissionsUser;
data['published_at'] = this.publishedAt;
data['created_at'] = this.createdAt;
data['updated_at'] = this.updatedAt;
return data;
}
}
The UserModel.fromJson method will only work if the response.body content is a User, so to fix your issue, instead of using UserModel.fromJson on the response.body json decoded variable, rather use the fromJson() function on data that you are sure will conform to your class definition.
For example:
class User{
final String name;
final String surname;
final String email;
User({this.name, this.surname, this.email});
factory User.fromJson(Map<String,dynamic> json){
return User(
'name': json['name'],
'surname': json['surname'],
'email': json['email']
);
}
}
The json response that is recieved from the api:
{
"name" : "John",
"surname": "Smith",
"email": "johnsmith#mail.com"
}
In your function, decode response and cast to the User model class:
final parsed = jsonDecode(response.body); // or Map<String,dynamic> parsed = jsonDecode(response.body);
User user = User.fromJson(parsed)
I want to sort each entity from following data in flutter
i.e enrollment_no,nationality,mother this data is coming from api
"personal":
"{\"enrollment_no\":\"1701\",
\"nationality\":\"INDIAN\",
\"driver_mobile\":\"-\",
\"mother\":\"JAGRUTIBAHEN SHRIKANT SONI\",
\"email\":\"SHRIKANT206#YAHOO.CO.IN\",
\"student_photo\":\"/container/school_data/BRS/photo/Student/1701.jpg\",
\"name\":\"NEYSA SHRIKANT SONI\",
\"mother_mobile\":\"+971507603564\",
\"father_mobile\":\"+971503171294\",
\"father\":\"SHRIKANT INDUKANT SONI\"}",
//I trying following code to sort data but can't achieve
if(personal == data['personal']) {
for (int i = 0; i < data['personal'].length; i++)
{
arrayp = personal;
print(arrayp);
var array1=arrayp[0]['father'];
print(array1);
}
}
1. Your JSON from API
{
"personal":
{
"enrollment_no": "1701",
"nationality": "INDIAN",
"driver_mobile": "-",
"mother": "JAGRUTIBAHEN SHRIKANT SONI",
"email": "SHRIKANT206#YAHOO.CO.IN",
"student_photo": "/container/school_data/BRS/photo/Student/1701.jpg",
"name": "NEYSA SHRIKANT SONI",
"mother_mobile": "+971507603564",
"father_mobile": "+971503171294",
"father": "SHRIKANT INDUKANT SONI"
}
}
2. Go To https://javiercbk.github.io/json_to_dart/
Convert your Json to Dart Classes.
class Personal {
PersonalData personal;
Personal({this.personal});
factory Personal.fromJson(Map<String, dynamic> json) {
return Personal(
personal: json['personal'] != null ?
PersonalData.fromJson(json['personal']) : null,
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.personal != null) {
data['personal'] = this.personal.toJson();
}
return data;
}
}
class PersonalData {
String driver_mobile;
String email;
String enrollment_no;
String father;
String father_mobile;
String mother;
String mother_mobile;
String name;
String nationality;
String student_photo;
PersonalData({this.driver_mobile, this.email, this.enrollment_no, this.father, this.father_mobile, this.mother, this.mother_mobile, this.name, this.nationality, this.student_photo});
factory PersonalData.fromJson(Map<String, dynamic> json) {
return PersonalData(
driver_mobile: json['driver_mobile'],
email: json['email'],
enrollment_no: json['enrollment_no'],
father: json['father'],
father_mobile: json['father_mobile'],
mother: json['mother'],
mother_mobile: json['mother_mobile'],
name: json['name'],
nationality: json['nationality'],
student_photo: json['student_photo'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['driver_mobile'] = this.driver_mobile;
data['email'] = this.email;
data['enrollment_no'] = this.enrollment_no;
data['father'] = this.father;
data['father_mobile'] = this.father_mobile;
data['mother'] = this.mother;
data['mother_mobile'] = this.mother_mobile;
data['name'] = this.name;
data['nationality'] = this.nationality;
data['student_photo'] = this.student_photo;
return data;
}
}
3. Now time for you api response
_getResponseFromApi() asyn{
var response = await http.post({your parameters})
var data = Personal.fromJson(json.decode(response.body));
var listOfPersonData = data.personal
}