I have seen your json and written some code, just to let you know that i have loaded your json locally.
[
{
"id": 13,
"mentor_id": 5,
"mentee_id": 184,
"status": null,
"session_count": 0,
"current_job": null,
"email": null,
"phone_call": null,
"video_call": null,
"face_to_face": null,
"created_at": "2020- 02 - 20 20: 37: 50",
"updated_at": "2020- 02 - 20 20: 37: 50"
},
{
"id": 14,
"mentor_id": 8,
"mentee_id": 184,
"status": null,
"session_count": 0,
"current_job": null,
"email": null,
"phone_call": null,
"video_call": null,
"face_to_face": null,
"created_at": "2020- 02 - 21 22: 39: 31",
"updated_at": "2020- 02 - 21 22: 39: 31"
}
]
Above is your JSON file
// To parse this JSON data, do
//
// final yourDataModel = yourDataModelFromJson(jsonString);
import 'dart:convert';
List<YourDataModel> yourDataModelFromJson(String str) => List<YourDataModel>.from(json.decode(str).map((x) => YourDataModel.fromJson(x)));
String yourDataModelToJson(List<YourDataModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class YourDataModel {
int id;
int mentorId;
int menteeId;
dynamic status;
int sessionCount;
dynamic currentJob;
dynamic email;
dynamic phoneCall;
dynamic videoCall;
dynamic faceToFace;
String createdAt;
String updatedAt;
YourDataModel({
this.id,
this.mentorId,
this.menteeId,
this.status,
this.sessionCount,
this.currentJob,
this.email,
this.phoneCall,
this.videoCall,
this.faceToFace,
this.createdAt,
this.updatedAt,
});
factory YourDataModel.fromJson(Map<String, dynamic> json) => YourDataModel(
id: json["id"],
mentorId: json["mentor_id"],
menteeId: json["mentee_id"],
status: json["status"],
sessionCount: json["session_count"],
currentJob: json["current_job"],
email: json["email"],
phoneCall: json["phone_call"],
videoCall: json["video_call"],
faceToFace: json["face_to_face"],
createdAt: json["created_at"],
updatedAt: json["updated_at"],
);
Map<String, dynamic> toJson() => {
"id": id,
"mentor_id": mentorId,
"mentee_id": menteeId,
"status": status,
"session_count": sessionCount,
"current_job": currentJob,
"email": email,
"phone_call": phoneCall,
"video_call": videoCall,
"face_to_face": faceToFace,
"created_at": createdAt,
"updated_at": updatedAt,
};
}
This is the model class i have made for your json
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:sample_project_for_api/model.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool _isLoading = false;
@override
void initState() {
super.initState();
loadYourData();
}
Future<String> loadFromAssets() async {
return await rootBundle.loadString('json/parse.json');
}
loadYourData() async {
String jsonString = await loadFromAssets();
final yourDataModel = yourDataModelFromJson(jsonString);
for (int i = 0; i < yourDataModel.length; i++) {
if (yourDataModel[i].id == 13) {
print('you got your id');
// Do your stuff
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(),
);
}
}
This is the main file in which the loadYourData method is doing the core parts.
Let me know if it works.