Home > Net >  flutter search from API Json
flutter search from API Json

Time:11-29

Im trying to make a searchbar that fetch data from mysql , but i have a problem that when i call data fetch function it returns Instance of '_Future<List>' this is my code

Future<List<Students>> getStudents(String id) async {
  var url = 'http://localhost/getStudents.php';
  final response = await http.post(Uri.parse(url), body: {
    'id':id,
  });
  var res = jsonDecode(response.body)['fname'] as List;
  if (response.statusCode == 200) {
    return res.map((std) => Students.fromJson(std)).toList();
  }else {
    throw Exception('Failed to load shows');
  }
}

my class :

class Students{
  final int id;
   final String fname;
   final String sname;
   final String tname;
  
const Students( {
  required this.id,
required this.sname,
 required this.tname, 
required this.fname,
});
   factory Students.fromJson(Map<String, dynamic> json) => Students(
     id: json['Id'],
     sname: json['secname'],
     tname:json['thirdname'] ,
     fname: json['firstname'],
   );

}

CodePudding user response:

getStudents is a future function and you need to await for it:

List<Students> result = await getStudents('');
  • Related