我遇到了Dart 1.16.0 + Polymer的奇怪问题。要执行计算,需要将double类型输入参数转换为字符串,然后再将其转换为double以进行计算,否则将引发无效的参数异常。任何人都可以指出我可能出错的地方吗?
HTML code, stats.html
<div class="container flex-horizontal">
<div class="flexchild-horizontal">
<div class="container flex-horizontal">
<div class="flexchild-horizontal">Base</div>
<div><input type="text" name="base" value="{{base::input}}" size="5" maxlength="5"></div>
</div>
</div>
<div class="flexchild-horizontal">
<div class="container flex-horizontal">
<div class="flexchild-horizontal">Calculated Value</div>
<div><input type="text" name="calc_value" value="{{calculateValue(base)}}" size="5" maxlength="5"></div>
</div>
</div>
Dart代码, stats.dart
@HtmlImport('stats.html')
library workbench.lib.client.stats;
import 'package:web_components/web_components.dart' show HtmlImport;
import 'package:polymer/polymer.dart';
import 'package:polymer_elements/paper_material.dart';
import 'package:polymer_elements/paper_styles.dart';
import 'package:polymer_elements/iron_flex_layout.dart';
@PolymerRegister('stats')
class Stats extends PolymerElement {
@property(notify: true)
double base = 10.0;
//@Property(computed: 'calculateValue(base)')
//double value;
Stats.created() : super.created();
@reflectable
double calculateValue(double b) {
print("Base is ");
print(b);
// Not working. Throw an invalid Argument: 5.0 Exception
// var c = b + 5.0;
// Working
var c = double.parse(b.toString()) + 5.0;
print("c is ");
print(c);
return c;
}
ready() {
}
}
答案 0 :(得分:2)
我猜你需要使用num
代替double
。
至少在Dartium中,据我所知5.0
将int
返回,而不是double
。这是因为在JS中,int
和double
之间没有区别。
int
和double
之间的正确区分仅适用于服务器(独立VM)。只要没有从JS传递值,它也适用于Dartium。