ike13
ike13

Reputation: 157

Flutter: Can't solved - type 'List<dynamic>' is not a subtype of type 'List<Files>'

I have a model

class Files {
  String fileName;
  bool type;

  Files(this.fileName, this.type);

  @override
  String toString() {
    return '{ ${this.fileName}, ${this.type} }';
  }
}

Next I define The list paths = [];

Then I run this line to add the file name and boolean type to it to find out whether the file is a directory or not

    for (final file in files) {
      print(file.path);
    paths.add(Files(file.name.toString(), file.isDirectory));
    }
    return paths.toList();

I get the following data: enter image description here

And when I start building a ListView with this code:

  Widget listViewWidget(List<Files> file) {
    return Container(
      child: ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          itemCount: file.length,
          itemBuilder: (context, position) {
            return Card(
              elevation: 8.0,
              margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
              child: Container(
                decoration: BoxDecoration(color: Color.fromRGBO(133, 123, 122, .9)),
                child: ListTile(
                  onTap: () {},
                  contentPadding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
                  title: Text(
                    file[position].type ? file[position].fileName+" This is a Folder" : file[position].fileName+" This is a file",
                    style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            );
          }
      ),
    );
  }

I get the following error: type 'List<dynamic>' is not a subtype of type 'List<Files>'

Can anyone suggest what the problem might be?

Upvotes: 0

Views: 293

Answers (2)

wafL
wafL

Reputation: 655

On a List, you can't insert 2 values. You can only insert a File object. Furthermore, you can't assume the type of a dynamic list to be of a class. dynamic should be of the "basic" types, like int, String, double or bool, because it might be dangerous to assume whole objects types.

What I suggest is:

List<File> paths = List<File>();

This way, you can do this:

paths.add(fileObject);

And remember, the fileObject has to be an object of the File class.

Upvotes: 1

MickaelHrndz
MickaelHrndz

Reputation: 3842

If you annotate the type for your function's parameter as List<Files>, you have to pass it exactly that. You don't show your call of listViewWidget() so I'm not sure where, but you're missing a type annotation.

If you're doing listViewWidget(paths), then declaring var paths = <Files>[]; should do the trick.

Upvotes: 1

Related Questions