Flutter - type 'List<dynamic>' is not a subtype of type 'List<File>'
459
Solution 1
Simply replace
List<File> viewImg = [].toList();
with this
List<File> viewImg = [];
Solution 2
When you say:
List<File> viewImg = [].toList();
You're actually creating a List<dynamic>
and assigning it to a List<File>
which is an error. What you can do is, either use List.from
List<File> viewImg = List<File>.from([].toList());
or
List<File> viewImg = <File>[].toList();
Those were just bad ways (because you were just copying the List) to do simple things, you should rather use:
List<File> viewImg = [];
or
var viewImg = <File>[];
Author by
Harry R
Updated on December 25, 2022Comments
-
Harry R over 1 year
I was trying to use a ListView.builder to display a list of Images dynamically as they are saved into an array of type File,
Here is my code :
List<File> viewImg = [].toList(); _imgFromGallery() async { File image = await ImagePicker.pickImage( source: ImageSource.gallery, imageQuality: 50); setState(() { _image = image; viewImg.add(image); }); } ListView.builder( itemCount: viewImg.length, itemBuilder: (BuildContext context, int index) { return new SingleChildScrollView( physics: ScrollPhysics(), child: Column(children: [ Container( margin:EdgeInsets.only(top30), width: size.width * .8, height: 149, decoration: BoxDecoration( image: DecorationImage( image:FileImage(viewImg[index]), )), ),
Every time I run it I get the error:
type 'List<dynamic>' is not a subtype of type 'List<File>'.
Please help me out and thanks in advance.
-
iDecode over 3 yearsThis won't do anything. And
List()
is now deprecated. -
Faiizii Awan over 3 years@iDecode List class is in dart.core. So far, I checked its docs and there is no indication of deprecated. can you please share the link ?
-
iDecode over 3 yearsHere you go stackoverflow.com/q/63451506/12483095
-
Faiizii Awan over 3 yearshaha :-D @iDecode & CopsOnRoad thanks, you guys saved me, since I have used default constructor at many places in my current ongoing project :-D
-
iDecode over 3 yearsNo worries, since you got an edit, my downvote doesn't stay relevant.