I'm a beginner in Flutter, and have the below Model,
@JsonSerializable()
class ModelSamejt {
String pid;
String grp;
String country;
String indust;
String job;
List<ModelUserGroup> content;
ModelSamejt(
{
required this.pid,
required this.grp,
required this.country,
required this.indust,
required this.job,
required this.content,
});
factory ModelSamejt.fromJson(Map<String, dynamic> json) =>
_$ModelSamejtFromJson(json);
Map<String, dynamic> toJson() => _$ModelSamejtToJson(this);
I'm receiving this Json from API:
[
{
"pid":1,
"grp":"Group1",
"country":"Egypt",
"indust":"Information Technology",
"job":"AP",
"content":[
{
"uid":2,
"puid":1,
"avatar":"http"://192.168.39.108/koko/image/user//scaled_dacdd0f1-a6e7-4334-a244-87ee52d233306652023795688525335.jpg,
"fname":abdo2,
"lname":"sakre",
"xp":10
},
And this is the Method used to bring data from API :
Future<List<ModelSamejt>> getSamejts(List<ModelSamejt> q) async {
var request = await Dio().get(Api.baseUrl + Api.api + Api.samejt);
print(request);
for (Map<String, dynamic> samejt in request.data) {
List<ModelUserGroup> mqc = [];
for (Map<String, dynamic> content in samejt['content']) {
mqc.add(ModelUserGroup(
uid: content['uid'],
puid: content['puid'],
avatar: content['avatar'],
fname: content['fname'],
lname: content['lname'],
xp: content['xp'],
));
}
q.add(ModelSamejt(
pid: samejt['pid'],
grp: samejt['grp'],
country: samejt['country'],
indust: samejt['indust'],
job: samejt['job'],
content: mqc));
}
return q;
}
I'm following a tutorial where the instructor filled a static array of his model with static data as follows. I need to do the same by filling my dynamic data coming from the API into an array. How to do this in Flutter?
static List<User> users = [
User(
id: 1,
name: 'John',
age: 25,
imageUrls: [
'https://images.unsplash.com/photo-1595623238469-fc58b3839cf6?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=755&q=80',
],
jobTitle: 'Job Title Here',
interests: ['Music', 'Economics', 'Football'],
bio:
'Sed ut perspiciatis
),