Flutter : Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<double>' in type cast

Viewed 271

I am getting following error:

E/flutter (21169): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: type 'List' is not a subtype of type 'List' in type cast

type of getPatientDataList() is Future<List<List<dynamic>> Function().

and I want to create a List<List<<double>>> i.e. sublist. But all time failed.

  getPatientDataList().then((result) {
  dataSize = result.length;

  List<List<double>> list = (result.getRange(0, 500).toList()).cast<List<List<double>>>();
  print("data " + list.toString());
});                         
2 Answers

try using

getPatientDataList().then((result) {
  dataSize = result.length;

  List<dynamic> list = (result.getRange(0, 500).toList()).cast<List<double>();
  print("data " + list.toString());

});

This is a task from the school course.

void main(List<String> args) {
  getPatientDataList().then((result) {
    final dataSize = result.length;
    final List<List<double>> list = result
        .getRange(1, 3)
        .map((e) => e.map((e) => e as double).toList())
        .toList();
    print("data " + list.toString());
  });
}

Future<List<List>> getPatientDataList() async {
  return <List<dynamic>>[
    [0.0, 0.5],
    [1.0, 1.5],
    [2.0, 2.5],
    [3.0, 3.5],
  ];
}

Output:

data [[1.0, 1.5], [2.0, 2.5]]

Or even...

void main(List<String> args) {
  getPatientDataList().then((result) {
    final dataSize = result.length;
    final List<List<double>> list =
        result.getRange(1, 3).map((e) => e.cast<double>()).toList();
    print("data " + list.toString());
  });
}

And so...

void main(List<String> args) {
  getPatientDataList().then((result) {
    final dataSize = result.length;
    final List<List<double>> list =
        List.of(result.getRange(1, 3).map((e) => e.cast()));   
    print("data " + list.toString());
  });
}
Related