Home > Enterprise >  Static Typing Hell
Static Typing Hell

Time:03-12

My background is in Python where you treat everything as a duck, and free from defining types. I recently started coding in Dart, and here I am facing these type casting issues...
What's wrong with the below code, if anyone could kindly point out?
What I am trying to do is that the appendCsv function takes a List<List<dynamic>> parameter, getMarks function returns a List<dynamic> list. Now I want to call the getMarks in a loop and append all the lists to another list e.g the x. And then pass that list to the appendCsv function.

List<List<dynamic>> x = [];
var v = await etea.getMarks('809', 70740) as Iterable<List<dynamic>>;
x.addAll(v);
print(x);
print(x.runtimeType);
// x = x as List<List<dynamic>>;
print(x.runtimeType);

appendCsv('802', x);

I am getting these kinds of mix errors.
Error:

type 'List<Iterable<String>>' is not a subtype of type 'Iterable<List<dynamic>>' in type cast

CodePudding user response:

If getMarks returns a List< dynamic> you just have to add it to x.

List<List<dynamic>> x = [];
for(what ever){
    List<dynamic> v = await etea.getMarks('809', 70740);
    x.add(v);
}

or even

List<List<dynamic>> x = [];
for(what ever){
    x.add(await etea.getMarks('809', 70740));
}

CodePudding user response:

Finally solved my issue, thanks to @AntEdote as well as @coolhack7's How to fix the error "'WhereIterable' is not a subtype of type 'List'"
The problem was in getMarks function it was returning a List<WhereIterable<String>> but I was treating it as List<dynamic>.
When you call .where on something, it returns a WhereIterable type, that's a stupid type name WhereIterable lol.
As described by coolhack7, I called .toList() on the WhereIterable<String> in getMarks and fixed the issue.

  • Related