Reputation: 832
I made a Json
post request and got a response that looks like this:
[{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}]
Now, I want to check if the id
key is equal to a certain number so I could set a button's state. For example:
if(id contains 5){
....
}
How do I achieve that in flutter?
Upvotes: 2
Views: 7035
Reputation: 3777
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.
Upvotes: 1
Reputation: 3055
You need to learn more about Maps
in the Dart language. Flutter is just a framework for making cross-platform apps, the language behind it and the logic you write is in the Dart language.
//You have a list of data, so first put it in a List variable.
//You need to first convert the list of raw json objects text response into a list of Maps.
List<Map> data = jsonDecode(httpResponse.body);
//Here we loop through the elements of the list
for (Map jsonObject in data) {
//This is how you get a value from a Map.
int id = jsonObject["id"];
if (jsonObject["id"] == 5){
print("ID is 5.");
} else {
print("ID is NOT 5.");
}
}
Hope this helps!
Upvotes: 0