admin管理员组文章数量:1410706
I am trying to learn client-side Dart, ing 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
?
I am trying to learn client-side Dart, ing 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
?
1 Answer
Reset to default 8There's no built-in way in js-interop for now to use Dart List
when a js Array
is returned.
directionsLeg.steps
returns a js.Proxy
which handles like js Array
. You can iterate on it like this :
final steps = directionsLeg.steps;
for (var i = 0; i < steps.length ; i++) {
final step = steps[i];
// do the job with step
}
If you really want to use a Dart List
you can convert the js.Proxy
of js Array
to a Dart List
with something like :
List<js.Proxy> convertToList(js.Proxy arrayProxy){
final result = new List<js.Proxy>();
for (var i = 0; i < arrayProxy.length ; i++) {
result.add(arrayProxy[i]);
}
return result;
}
About your code :
- You can't define
List<maps.DirectionsStep>
:maps.DirectionsStep
is not a type, it's ajs.Proxy
on jsgoogle.maps.DirectionsStep
(moreover it don't really exist - only a container js object{}
). List.from(...)
: here, you try to call a static method namedfrom
on DartList
object. That why you get your error.List.from
is actually a factory named constructor and has to be used with thenew
keyword (new List.from(otherIterable)
).
本文标签: Creating a Dart List from a Javascript arrayStack Overflow
版权声明:本文标题:Creating a Dart List from a Javascript array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744825088a2627010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论