Arithmetic operation in Dart+Polymer throws an invalid argument exception

Issue

I encountered an odd problem with Dart 1.16.0 + Polymer. To perform calculation, the double type input parameter need to be converted to string and then back again to double for computation, otherwise an invalid argument exception will be thrown. Could anyone point out where I might have gone wrong?

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 code, 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() {

  }
}

Solution

I guess you need to use num instead of double.
At least in Dartium as far as I know 5.0 would be returned as int, not as double. This is because in JS there is no distinction between int and double possible.

Proper distinction between int and double is only working on the server (standalone VM). It also works in Dartium as long as the value is not passed from JS.

Answered By – Günter Zöchbauer

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *