Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

type 'List<BannerPic>' is not a subtype of type 'List<String>'

im calling an API which returns a lot of picture that i call it like this

Future<List<BannerPic>> _getGalleryBanner() async {
var data = await MainApi("url","", '', '', "getBannerImage");
var jsonData = json.decode(data);
var jsonParse = jsonData["data"];
List<BannerPic> bannerpic = [];

for (var u in jsonParse) {

  BannerPic bannerPic = BannerPic(
      u["number"],
      u["Picture"]
  );
  bannerpic.add(bannerPic);
}

return bannerpic;
}

and im trying to put it on a carousel, but i got an error like this :

type 'List<BannerPic>' is not a subtype of type 'List<String>'

and this is how i use the api feedback

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FutureBuilder(
                            future: _getGalleryBanner(),
                            builder: (context, snapshot) {
                              if (snapshot.hasData) {
                                return CarouselImages(
                                  scaleFactor: 0.7,
                                  listImages: snapshot.data,
                                  height: 300.0,
                                  borderRadius: 30.0,
                                  cachedNetworkImage: true,
                                  verticalAlignment: Alignment.bottomCenter,
                                );
                              } else {
                                return DecoratedBox(
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(1.h)
                                    )
                                );
                              }
                            },
                          )

i thought of because its already on List form i could just use it since it return :

List<BannerPic> bannerpic = [];

but i cant use it since it was not a type 'List<String>'

>Solution :

You have to get image url from the API.
try this code

Future<List<String>> _getGalleryBanner() async {
 var data = await MainApi("url","", '', '', "getBannerImage");
 var jsonData = json.decode(data);
 var jsonParse = jsonData["data"];
 List<String> bannerpic = [];
 for (var u in jsonParse) { 
  bannerpic.add(u["Picture"].toString);
 }
 return bannerpic;
}

FutureBuilder(
                        future: _getGalleryBanner(),
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                            return CarouselImages(
                              scaleFactor: 0.7,
                              listImages: snapshot.data,
                              height: 300.0,
                              borderRadius: 30.0,
                              cachedNetworkImage: true,
                              verticalAlignment: Alignment.bottomCenter,
                            );
                          } else {
                            return DecoratedBox(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(1.h)
                                )
                            );
                          }
                        },
                      )

Since you are need list of image URLs for CarouselImages
*make sure u["Picture"] contains image url.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading