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'
CodePudding user response:
Remove the Future
from getOtherUserPhoto()
notation.
CodePudding user response:
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;
}