How to get "plate" value in such object? Also how can I get value inside 'fixation', because it is list?
[
{
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"car": {
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"plate": "string",
"fixations": [
"string"
]
},
"fixed": "2022-08-17T09:55:33.709Z",
"speed": 0,
"latitude": 0,
"longitude": 0,
"upload": true,
"image": {
"id": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"file": "string"
}
}
]
I write code like this:
body: FutureBuilder<List>(
future: AllFixation().fetchAllFixation(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: snapshot.data?.length ?? 0,
itemBuilder: (context, index) {
return Text(snapshot.data?[index]["upload"].toString() ?? '');
});
},
),
CodePudding user response:
For this situation, you should write a class that implements a .fromMap() method to comfortable handle data. But, if your want to use a "raw" data, you should be carefull with such constructions. In your case, you can get "plate" value like this:
snapshot.data?[index]["car"]["plate"] as String;
To get fixation, you should follow this snippet:
var myList = snapshot.data?[index]["car"]["fixations"].cast<String>();
Then, you could work with myList
as usual List<String>
.
I used .cast<String>()
instead of as List<String>
, because in some situations, "as List<String>
" will throw Exception like "List<String> is not subtype of List<dynamic>
"