I try to make filter list data in flutter to filter the id or name from api data but I get every time this error:
Class 'int' has no instance getter 'id'.
Receiver: 1171
Tried calling: id
My page code:
class testpage extends StatefulWidget {
const testpage({Key? key}) : super(key: key);
@override
_testpageState createState() => _testpageState();
}
class _testpageState extends State<testpage> {
var apiURL;
@override
void initState() {
Searchgetdata();
super.initState();
}
List listsearch = [];
Future Searchgetdata() async {
apiURL =
'**************';
var response = await http.post(Uri.parse(apiURL));
setState(() {
List jsonList = jsonDecode(response.body);
for (int i = 0; i < jsonList.length; i ) {
listsearch.add(jsonList[i]['id']);
print(listsearch);
}
});
}
@override
Widget build(BuildContext context) {
final _filteredList = listsearch.where((product) => product.id == '1202').toList();//here problem......
print(_filteredList);
return MaterialApp(home: Scaffold(
body: ListView(
children: [
]
),
),
);
}
}
I'm sure the api connection is correct and the data is fetched through it.
Update this is print data what I get from api: I make this line like that to just print id:
listsearch.add(jsonList[i]['id']);
then I get this data:
I/flutter ( 4792): [695, 523, 908, 522, 987, 727, 1162, 1586, 862, 746, 780, 916, 1428, 1218, 686, 745, 942, 902, 829, 851, 751, 792, 713, 900, 910, 532, 698, 913, 690, 668, 836, 663, 1221, 849, 928, 679, 898, 852, 909, 1229, 1223, 824, 742, 875, 1169, 1407, 1029, 901, 611, 893, 1203, 1202, 1340, 688, 681, 1215, 1170, 922, 895, 1036, 724, 1211, 1249, 737, 1224, 872, 1220, 1168, 796, 662, 682, 822, 670, 680, 669, 1409, 789, 856, 912, 689, 517, 1410, 897, 605, 921, 1138, 612, 1163, 1263, 741, 1226, 1219, 904, 783, 1228, 725, 1213, 864, 795, 1204, 903, 1276, 613, 1206, 1433, 965, 1216, 696, 527, 714, 1230, 981, 736, 894, 610, 672, 609, 854, 790, 691, 914, 855, 966, 896, 666, 723, 831, 797, 687, 722, 726, 674, 1201, 850, 1171, 738, 739, 1099, 899, 848, 671, 684, 964, 743, 1255, 787, 1172, 694, 1118, 1227, 750, 870, 566, 525, 915, 1100, 917, 1342, 1205, 1408, 667, 606, 794, 665, 1113, 608, 847, 536, 853, 685, 1160, 607, 675, 699, 516, 906, 1225, 1212, 865, 866, 863, 740, 701, 673, 907, 728, 1431, 911, 1222, 1256, 712, 683, 748, 121
anyone can help me thanks you
CodePudding user response:
Your listsearch
is a list of ids, not of products.
listsearch.add(jsonList[i]['id']);
You add the id of the item to the jsonList, not the id itself, but here
final _filteredList = listsearch.where((product) => product.id == '1202').toList();//here problem......
You try to access the .id
again. An id (integer) does not have an .id
attribute!
If you want to add every object to your list, then search for a specific id you should do
listsearch.add(jsonList[i]);
And then you can search for the object with an ['id']
attribute equal to 1202.