简体   繁体   中英

How to call CachedNetworkImageProvider's list method into carousel pro in Flutter

I want to use the images with the CachedNetworkImageProvider list inside the carousel İn Flutter.

But I am getting this error type

Can you help on the subject?

Edit

  Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.black,
  appBar: AppBar(
    backgroundColor: Colors.black,
    title: Text("Home"),
  ),
  body: ListView(
    children: [
      SizedBox(
        height: 200.0,
        width: double.infinity,
        child: Carousel(
          images: [
            //CachedNetworkImageProvider('https://cdn-images-1.medium.com/max/2000/1*GqdzzfB_BHorv7V2NV7Jgg.jpeg'),
            //CachedNetworkImageProvider('https://cdn-images-1.medium.com/max/2000/1*wnIEgP1gNMrK5gZU7QS0-A.jpeg'),
            getOtherUserPhoto(),
          ],
        ),
      ),
    ],
  ),
);

}

  Future<List<CachedNetworkImageProvider>> getOtherUserPhoto() async {

try{
  List<CachedNetworkImageProvider> _userPhotosWidgetList = [];
  final _userModel = Provider.of<OtherUserViewModel>(context, listen: false);
  List<String> _userPhotosList = await _userModel.getOtherUserPhoto();
  for(int i = 0; i < _userPhotosList.length; i++){
    _userPhotosWidgetList.add(CachedNetworkImageProvider(_userPhotosList[i].toString()));
  }

  return _userPhotosWidgetList;

}catch(e){
  print("Exception cause : " + e.toString());
  return null;
}

}

Error Logs:

======== Exception caught by widgets library ======================================================= The following _TypeError was thrown building Carousel(dirty, state: CarouselState#3294f): type 'Future<List>' is not a subtype of type 'Widget'

You can not assign a future directly to a list, you have to wait for a response, so we'll use a FutureBuilder like this:

     SizedBox(
        height: 200.0,
        width: double.infinity,
        child: FutureBuilder<List<Widget>>(
            future: getOtherUserPhoto(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
               return LinearProgressIndicator();
              }
              return Carousel(images: snapshot.data);
            }),
      ),

and you need to change your function type, since CachedNetworkImageProvider is not a widget, instead use CachedNetworkImage:

  Future<List<Widget>> getOtherUserPhoto() async {
  List<CachedNetworkImage> _userPhotosWidgetList = [];
   final _userModel = Provider.of<OtherUserViewModel>(context, listen: false);
  List<String> _userPhotosList = await _userModel.getOtherUserPhoto();
  for(int i = 0; i < _userPhotosList.length; i++){
    _userPhotosWidgetList.add(CachedNetworkImage(imageUrl: _userPhotosList[i].toString()));
  }
 return _userPhotosWidgetList;
}

Remove the Future from getOtherUserPhoto() notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM