I want to use where method in dynamic array. I am getting data from api and i want to find some data from the response with the help of where method.
Following are the repsonse of api.
[
{
'status': true,
'result': 'found',
'data': [
{
'id': 10676487174744,
'name': 'Taki Rajani',
'email': '[email protected]',
'isAdmin': false,
'description': 'ta rai'
},
{
'id': 1172269813061229,
'name': 'Aar Raj',
'email': '[email protected]',
'isAdmin': false,
'description': 'test'
},
{
'id': 12854121,
'name': 'testing',
'email': '[email protected]',
'isAdmin': false,
'description': 'details'
}
]
}
]
following are my code for finding same user id which i was stored in storeUserID variable.
2 problems:- 1)when i print listof user, I am getting all datas in my case 3 data but when I print length of this list the reponse shows count only 1. 2)"where" method is showing nothing on print might be is not working.
Future<GetUserData> getUserDetail() async {
var url = "https://aeliya.mydomin.com/getUser.php";
var response = await http.get(Uri.parse(url));
var jsondata = jsonDecode(response.body.toString());
List listOfUser = [];
if (response.statusCode == 200) {
//print(jsondata);
listOfUser.add(jsondata);
print(listOfUser);
print(listOfUser.length);
var findid = listOfUser.where((element) => element == storeUserID);
print(findid);
return GetUserData.fromJson(jsondata);
} else {
return GetUserData.fromJson(jsondata);
}
CodePudding user response:
Your api response contain a list and inside that the data contains the list you want. So change this:
if (response.statusCode == 200) {
//print(jsondata);
listOfUser.add(jsondata);
print(listOfUser);
print(listOfUser.length);
var findid = listOfUser.where((element) => element == storeUserID);
print(findid);
return GetUserData.fromJson(jsondata);
} else {
return GetUserData.fromJson(jsondata);
}
to this:
if (response.statusCode == 200) {
print(jsondata[0]['data']);
print(jsondata[0]['data'].length);
var findid = (jsondata[0]['data'] as List)
.where((element) => element['id'] == 1172269813061229);
print(findid);
return GetUserData.fromJson(jsondata[0]['data']);
} else {
return GetUserData.fromJson(jsondata[0]['data']);
}
CodePudding user response:
when I print length of this list the reponse shows count only 1
its because here listOfUser.add(jsondata);
you are adding the whole response body. and your response body is 1 object.
"where" method is showing nothing on print might be is not working.
you list is not a User
list, thats why this condition element == storeUserID
in your where
method will always false. And if its false, then it will return nothing.
workaround:
...
var jsondata = jsonDecode(response.body); // do not make it to String. Your response is a LIST
List listOfUser = [];
if (response.statusCode == 200) {
// i assume your response will only have 1 object
final data = jsondata[0]['data'];
listOfUser.add(data);
final findid = listOfUser.where((element) => element['id'] == storeUserID);
print(findid);
return GetUserData();
// return GetUserData.fromJson(jsondata); you have to make sure this is not error. its because your response is an array. not a Map
}else{
throw Exception(response.body);
}
...