mauricio
mauricio

Reputation: 184

Expected a value of type 'int', but got one of type 'String'

I am working with this api: https://api.jikan.moe/v4/anime what I want is to obtain both the movies (images) and their titles. before I was just displaying the images inside a horizontal listview.builder and everything was going great. but when wanting to put their respective titles under each image I got the error mentioned in the title.

enter image description here

final list = data['data'] as List;
      //final list = data['data']["title"] as List;
      //return list.map((e) => DataApi(image: e)).toList();
      return list
          .map((e) => DataApi(
              image: e["images"]["jpg"]["image_url"],
              title: e["titles"]["title"]))
          .toList();

the way I intend to display them.

if (snapshot.hasData) {
          final lista = snapshot.data!;
          return Padding(
            padding: const EdgeInsets.symmetric(vertical: 20),
            child: Container(
              width: double.infinity,
              height: 200,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: lista.length,
                itemBuilder: (BuildContext context, int index) {
                  return Column(
                    children: [
                      Image.network(
                        lista[index].image,
                      ),
                      Text(lista[index].title),
                    ],
                  );
                },
              ),

As I said, what I intend is to put each name of the movie under its image. Please, I would really appreciate it. Thank you

Upvotes: 0

Views: 98

Answers (1)

Anas Hafidi
Anas Hafidi

Reputation: 425

titles are inside an array , that's why you need to acces each item on it

Change

title: e["titles"]["title"]

to

title: e["titles"][0]["title"] // this will get you  "Cowboy Bebop"

Upvotes: 2

Related Questions