Dart2js warnings when using ElementList length and []

Issue

I am new to Dart. To become familiar with Dart, I created this script. It works fine with the editor and pub serve. But pub serve dart2js logs these two warnings, which I do not understand:

  1. No member named ‘length’ in class ‘ElementList’
  2. No operator ‘[]’ in class ElementList

coloring.dart:

import 'dart:math';
import 'dart:html';
import 'dart:async';

const String CLICK_ME = 'Click me to pause!';

void randomColoring(StreamSubscription subscription) {

  final Random random = new Random();
  final Element intro = querySelector("#intro");
  final ElementList<Element> boxes = querySelectorAll('.box');
  final Element info = querySelector("#info");

  subscription.onData((eventValue) {
    var box = random.nextInt(boxes.length);                      /// WARNING
    var hexCssColor = random.nextInt(0xFFFFFF + 1).toRadixString(16)
        .padLeft(6, "0").toUpperCase();
    boxes[box].style.background = "#$hexCssColor";               /// WARNING
    info.text = eventValue.toString();
    print("box: $box - hexCssColor: $hexCssColor - count: $eventValue");
  });

  var text = intro.text;
  intro
      ..text = CLICK_ME
      ..onClick.listen((mouseEvent) {
        if (subscription.isPaused) {
          intro.text = CLICK_ME;
          subscription.resume();
        } else {
          intro.text = text;
          subscription.pause();
        }
      });
}

Solution

There is a bug for the [] operator of ElementList https://code.google.com/p/dart/issues/detail?id=19628 (fixed at June 26).

What Dart version are you using?

I wasn’t able to find something about length but I guess it’s a bug in Dart2JS.
You can report it at http://dartbug.com/new
But maybe the fix for the [] bug applies here as well.

Answered By – Günter Zöchbauer

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

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