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>[];
Share:
459
Harry R
Author by

Harry R

Updated on December 25, 2022

Comments

  • Harry R
    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
    iDecode over 3 years
    This won't do anything. And List() is now deprecated.
  • Faiizii Awan
    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
    iDecode over 3 years
  • Faiizii Awan
    Faiizii Awan over 3 years
    haha :-D @iDecode & CopsOnRoad thanks, you guys saved me, since I have used default constructor at many places in my current ongoing project :-D
  • iDecode
    iDecode over 3 years
    No worries, since you got an edit, my downvote doesn't stay relevant.