Home > Software design >  Unhandled Exception: type 'List<Widget>' is not a subtype of type 'Iterable<
Unhandled Exception: type 'List<Widget>' is not a subtype of type 'Iterable<

Time:10-08

After migrating to null safety, the migration tool added as Iterable<SetWidget> to my code. But unfortunately i get this error:

Unhandled Exception: type 'List<Widget>' is not a subtype of type 'Iterable<SetWidget>' in type cast

This is the code:

  final List<Widget> _sets = [];

  Map getSets() {
    Map sets = {};
    int k = 0;
    for (SetWidget set in _sets as Iterable<SetWidget>) {
      sets.putIfAbsent(k.toString(), () => set.getMap());
      k  ;
    }
    return sets;
  }

What is the issue here?

CodePudding user response:

I don't know why the tool decided to do that, but if you think about it, it obviously throws an error. _sets is a list of Widgets, which means it could include for example a Column, a Container, and a SetWidget, so it can't be cast to a SetWidget iterable.

is getMap() a method in the SetWidget class? if it is, could you change the List<Widget> to a List<SetWidget> and remove the as Iterable<SetWidget>? Hopefully, that solves your problem.

Also, side note, what is this code for? Are you turning a list into a Map<String, Function> where the string is just an index? if you are, why not use a list instead?

for (SetWidget widget in _sets)
{
  sets.add(widget.getMap)
}
  • Related