I am trying to learn client-side Dart, coming from a server-side Java EE world, and I am having trouble converting an array from an existing JavaScript library into a Dart List.
I am trying to learn by building on the Javascript Interop example that uses Google Maps. In Google’s Maps API’s documentation, the DirectionsLeg object’s step property returns.
array of DirectionsSteps, each of which contains information about the individual steps in this leg
How can I convert this var into a Dart List? I have tried the following:
final List<maps.DirectionsStep> steps = List.from(directionsLeg.steps);
But Dart Editor tells me cannot resolve method 'from' in class 'List'. My imports are:
import 'dart:html';
import 'dart:core';
import 'package:js/js.dart' as js;
What am I doing wrong? Is it even possible or must I just accept using a var?
There’s no built-in way in js-interop for now to use Dart
Listwhen a jsArrayis returned.directionsLeg.stepsreturns ajs.Proxywhich handles like jsArray. You can iterate on it like this :If you really want to use a Dart
Listyou can convert thejs.Proxyof jsArrayto a DartListwith something like :About your code :
List<maps.DirectionsStep>:maps.DirectionsStepis not a type, it’s ajs.Proxyon jsgoogle.maps.DirectionsStep(moreover it don’t really exist – only a container js object{}).List.from(...): here, you try to call a static method namedfromon DartListobject. That why you get your error.List.fromis actually a factory named constructor and has to be used with thenewkeyword (new List.from(otherIterable)).