Dart+Polymer中算术运算抛出参数无效异常
Arithmetic operation in Dart+Polymer throws an invalid argument exception
我在使用 Dart 1.16.0 + Polymer 时遇到了一个奇怪的问题。计算时需要将double类型的入参转为string再转回double进行计算,否则会抛出参数无效异常。谁能指出我可能哪里出错了?
HTML代码,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>
飞镖代码,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() {
}
}
我想您需要使用 num
而不是 double
。
至少在 Dartium 中,据我所知 5.0
将返回为 int
,而不是 double
。这是因为在 JS 中没有区分 int
和 double
的可能。
int
和 double
之间的正确区别仅适用于服务器(独立 VM)。只要值不是从 JS 传递的,它也适用于 Dartium。
我在使用 Dart 1.16.0 + Polymer 时遇到了一个奇怪的问题。计算时需要将double类型的入参转为string再转回double进行计算,否则会抛出参数无效异常。谁能指出我可能哪里出错了?
HTML代码,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>
飞镖代码,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() {
}
}
我想您需要使用 num
而不是 double
。
至少在 Dartium 中,据我所知 5.0
将返回为 int
,而不是 double
。这是因为在 JS 中没有区分 int
和 double
的可能。
int
和 double
之间的正确区别仅适用于服务器(独立 VM)。只要值不是从 JS 传递的,它也适用于 Dartium。