type 'List<List<dynamic>>' is not a subtype of type 'List<List<num>>' in type cast
Sacchid opened this issue · 2 comments
What is correct way to cast List<List<dynamic>>
to List<List<num>>
?
As, if I cast using .cast()
, I am getting following CastError
-
type
List<dynamic>
is not a subtype of typeList<num>
in type cast
And if I also use as List<List<num>>
, I am getting following CastError
-
type
List<List<dynamic>>
is not a subtype of typeList<List<num>>
in type cast
Details -
I have function List<Point> toListOfPoint(List<List<num>> list_of_list){...}
from import 'package:poly/poly.dart';
So, In order to pass dynamic list I'm casting List<List<dynamic>>
to List<List<num>>
But, I'm getting following error(s)-
-
type
List<dynamic>
is not a subtype of typeList<num>
in type cast
with code -
import 'package:poly/poly.dart';
main(){
List correctDynamicList = [1, 2];
// type `List<dynamic>` is not a subtype of type `List<num>` in type cast
try {
var l = [correctDynamicList];
// `l.runtimetype` = `List<List<dynamic>>`
print("${l.runtimeType}");
List<Point> wrongListOfList = toListOfPoint(l.cast());
} on CastError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
}
}
- If I use
as List<List<num>>
I get following error -
type
List<List<dynamic>>
is not a subtype of typeList<List<num>>
in type cast
with following code -
import 'package:poly/poly.dart';
main(){
List correctDynamicList = [1, 2];
// type `List<dynamic>` is not a subtype of type `List<num>` in type cast
try {
var l = [correctDynamicList] as List<List<num>>;
List<Point> wrongListOfList = toListOfPoint(l.cast());
} on CastError catch (e) {
print(
"${++exception_count}. ${e.runtimeType} Exception handled : \n \t \u2022 ${e}");
}
}
- Dart SDK Version (
2.2.0
) - using Windows
One thing you need to take into account is that List<List<dynamic>>
is not a subtype of List<List<num>>
. So if you have a List<List<dynamic>>
(not just an object o
such that o is List<List<dynamic>>
is true, but such that List<List<dynamic>>
is the most specific type that o
has) then there is no cast that will allow you to assign that object to a variable of type List<List<num>>
. Just like there is no cast which will allow you to assign a String
to a variable of type bool
(you can make it a run-time error rather than a compile-time error by inserting a cast to dynamic
, but it will fail).
You can obtain a view on a List<List<dynamic>>
of type List<List<num>>
by calling cast<List<num>>()
on it. This will dynamically check that it actually contains instances of List<num>
, and throw if any of them isn't. So you may then need to do something similar for each of the nested lists, if they are actually of type List<dynamic>
.
The best approach would usually be to create your lists with the required type from the very beginning. Clients who don't care can then work on it using a type like List<List<dynamic>>
, and clients who really insist on getting a List<List<num>>
would get what they need.
What is correct way to cast
List<List<dynamic>>
toList<List<num>>
?
If you want to convert from one to other (and can't arrange for the original to be of the right type), the following will do what you want:
List<List<dynamic>> l = [[3]];
// Map over the list, casting each element list.
List<List<num>> ln = l.map((x) => x.cast<num>()).toList();