William
William

Reputation: 984

List from json API response

I'm trying to use https://pub.dev/packages/flappy_search_bar#-readme-tab- to create a list of data which I plan on getting from an api(just testing now), but I can't seem to add the list created from the response json to the search widget.

The argument type 'Future<List<Album>> Function()' can't be assigned to the parameter type 'Future<List<Album>> Function(String)'.
class Album {
  final int userId;
  final int id;
  final String title;

  Album({this.userId, this.id, this.title});

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      userId: json['userId'],
      id: json['id'],
      title: json['title'],
    );
  }
}
  Future<List<Album>> fetchAlbum() async {
    final response =
        await http.get('https://jsonplaceholder.typicode.com/albums/1');

    if (response.statusCode == 200) {
      // If the server did return a 200 OK response,
      // then parse the JSON.
      List jsonResponse = json.decode(response.body);
      return jsonResponse.map((job) => Album.fromJson(job)).toList();
    } else {
      // If the server did not return a 200 OK response,
      // then throw an exception.
      throw Exception('Failed to load album');
    }
  }
  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: SearchBar<Album>(
          onSearch: fetchAlbum, <------ error here
          onItemFound: (Album post, int index) {
            return ListTile(
              onTap: () => widget.setProviderData(post.title),
              title: Text(post.title),
              subtitle: Text(post.id.toString()),
            );
          },
        ),
      ),
    );
  }

Can anyone help me with this, please?

Upvotes: 1

Views: 525

Answers (1)

i6x86
i6x86

Reputation: 1627

I can't test your code right now, but at first glance the problem is onSearch expects a function that gets String parameter and your code doesn't provide it.

You should modify this line Future<List<Album>> fetchAlbum() async as follows:

Future<List<Album>> fetchAlbum(String album) async 

Upvotes: 1

Related Questions