Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type 'List<List<dynamic>>' is not a subtype of type 'List<List<num>>' in type cast #36614

Closed
Sacchid opened this Issue Apr 13, 2019 · 2 comments

Comments

Projects
None yet
4 participants
@Sacchid
Copy link

Sacchid commented Apr 13, 2019

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 type List<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 type List<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)-

  1. type List<dynamic> is not a subtype of type List<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}");
  }
}

  1. If I use as List<List<num>> I get following error -

type List<List<dynamic>> is not a subtype of type List<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

Sacchid added a commit to Sacchid/poly that referenced this issue Apr 13, 2019

+optimize imports
import 'dart:math' show Point;
import 'dart:convert' show utf8;
- Optimized imports
- Added `Polygon.hasSamePoint()`
- [Issue raised for `List<List<dyanamic>>` to `List<List<num>>` casting exception](dart-lang/sdk#36614 "Dart-lang List<List<dyanamic>> to List<List<num>> Casting Issue")
@eernstg

This comment has been minimized.

Copy link
Member

eernstg commented Apr 14, 2019

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.

@leafpetersen

This comment has been minimized.

Copy link
Member

leafpetersen commented Apr 15, 2019

What is correct way to cast List<List<dynamic>> to List<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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.