Harry R
Harry R

Reputation: 107

Flutter - type 'List<dynamic>' is not a subtype of type 'List<File>'

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.

Upvotes: 2

Views: 884

Answers (2)

CopsOnRoad
CopsOnRoad

Reputation: 267724

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>[];

Upvotes: 1

Faiizii Awan
Faiizii Awan

Reputation: 1710

Simply replace

List<File> viewImg = [].toList();

with this

List<File> viewImg = [];

Upvotes: 1

Related Questions