List of flutter images

Asked

Viewed 509 times

0

In my flutter app I want to display images, and these images will be grouped by categories/albums. These images are brought in the form of an array, in this format:

inserir a descrição da imagem aqui

This is my current function to bring these images

class ImagensApiProvider {
  Future<List<Imagens>> imagens() async {
    final response = await Dio().get(
      ApplicationConsts.BASE_API_URI + 'images/' + '3/' + '20',
    );

    if (response.statusCode == 200) {
      final parsed = response.data.cast<Map<String, dynamic>>();
      return parsed.map<Imagens>((json) => Imagens.fromJson(json)).toList();
    } else {
      throw Exception('Falha ao carregar Imagens');
    }
  }
}

'/3' and '20' are respectively the user id and the album id (this I resolve later).

The fact is that I came across the following mistake:

inserir a descrição da imagem aqui

And in Sponse.data I get the following:

inserir a descrição da imagem aqui

How do I fix it?

From now on, I appreciate any help!

  • If my answer has helped you or even solved your problem, please mark it as accepted, of course, if you are not expecting other answers.

1 answer

4


How do you already get a list of Strings, there’s no reason to turn her into a map, work directly on her:

Subistitua that

final parsed = response.data.cast<Map<String, dynamic>>();
return parsed.map<Imagens>((json) => Imagens.fromJson(json)).toList();

therefore

return response.data.map<Imagens>((json) => Imagens.fromJson(json)).toList();
  • now to get this error _Typeerror (type 'String' is not a subtype of type 'Map<String, Dynamic>'

  • Probably your method fromJson() is expecting to receive a Map<String, dynamic>, make the changes on it as now it will receive a String directly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.