dart2js js code `undefined is not a function`

Issue

I’m trying to compile in javascript my project.

Compilation works, but in browser (chrome) I have:

Uncaught TypeError: undefined is not a function client.dart.js:14753
J.get$length$asx client.dart.js:14753
InputStream.InputStream$4$byteOrder$length$start input_stream.dart:19
InputStream.static.InputStream$ input_stream.dart:17
DartAgnanAsk__get_data_closure.call$1 bzip2_decoder.dart:9
invokeClosure_closure0.call$0 js_helper.dart:1822
_IsolateContext.eval$1 isolate_helper.dart:355
_callInIsolate isolate_helper.dart:30
invokeClosure js_helper.dart:1822
(anonymous function)

For compilation I use this command:

dart2js -v --out=client.dart.js main.dart

And dart2js -v --analyze-all --out=client.dart.js main.dart give:

Info: Option '--analyze-all' implies '--analyze-only'.
Info: Options [--out=client.dart.js] indicate that output is expected, but compilation is turned off by the option '--analyze-only'.
Info: Package root is file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Documents/GitHub/DartAgnan/web/dart/packages/
Info: Allowed library categories: {Client, Shared, Internal}
Info: Scanning library dart:_js_helper (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/_internal/lib/js_helper.dart)
Info: Scanning library dart:core (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/core/core.dart)
Info: Scanning library dart:_internal (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/internal/internal.dart)
Info: Scanning library dart:_js_primitives (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/_internal/lib/js_primitives.dart)
Info: Scanning library dart:_foreign_helper (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/_internal/lib/foreign_helper.dart)
Info: Scanning library dart:_interceptors (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/_internal/lib/interceptors.dart)
Info: Scanning library dart:collection (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/collection/collection.dart)
Info: Scanning library dart:math (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/math/math.dart)
Info: Scanning library dart:convert (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/convert/convert.dart)
Info: Scanning library dart:async (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/async/async.dart)
Info: Scanning library dart:_isolate_helper (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/_internal/lib/isolate_helper.dart)
Info: Scanning library dart:isolate (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/isolate/isolate.dart)
Info: Scanning library dart:_js_names (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/_internal/lib/js_names.dart)
Info: Analyzing file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Documents/GitHub/DartAgnan/web/dart/main.dart (1.3.6)
Info: Scanning library file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Documents/GitHub/DartAgnan/web/dart/main.dart
Info: Scanning library package:route_hierarchical/client.dart
Info: Scanning library dart:html (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/html/dart2js/html_dart2js.dart)
Info: Scanning library dart:html_common (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/html/html_common/html_common_dart2js.dart)
Info: Scanning library dart:web_gl (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/web_gl/dart2js/web_gl_dart2js.dart)
Info: Scanning library dart:_native_typed_data (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/typed_data/dart2js/native_typed_data_dart2js.dart)
Info: Scanning library dart:typed_data (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/typed_data/dart2js/typed_data_dart2js.dart)
Info: Scanning library file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/html/html_common/metadata.dart
Info: Scanning library dart:indexed_db (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/indexed_db/dart2js/indexed_db_dart2js.dart)
Info: Scanning library dart:svg (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/svg/dart2js/svg_dart2js.dart)
Info: Scanning library dart:web_audio (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/web_audio/dart2js/web_audio_dart2js.dart)
Info: Scanning library dart:web_sql (file:///Volumes/Data/nfs/zfs-student-4/users/2013/tcaron/Dart/Dart tools/dart/dart-sdk/lib/web_sql/dart2js/web_sql_dart2js.dart)
Info: Scanning library package:logging/logging.dart
Info: Scanning library package:collection/wrappers.dart
Info: Scanning library package:route_hierarchical/src/utils.dart
Info: Scanning library package:route_hierarchical/link_matcher.dart
Info: Scanning library package:route_hierarchical/click_handler.dart
Info: Scanning library package:route_hierarchical/url_matcher.dart
Info: Scanning library package:route_hierarchical/url_template.dart
Info: Scanning library package:DartAgnan/module/initMod.dart
Info: Scanning library package:DartAgnan/module/home/controller/homeCtrl.dart
Info: Scanning library package:DartAgnan/module/ask/controller/DartAgnanAsk.dart
Info: Scanning library package:archive/archive.dart
Info: Scanning library package:crypto/crypto.dart
Info: Resolving...
Info: Resolved 1495 elements.
Info: Resolved 4746 elements.
Info: Preserving names.
Info: Resolved 14467 elements.
Info: Resolved 50 native elements used, 531 native elements dead.
Hint: 1 warning(s) suppressed in package:route_hierarchical.
Info: LibraryLoader took 63msec
Info: Scanner took 195msec
Info: Diet Parser took 115msec
Info: Parser took 607msec
Info: Patching Parser took 16msec
Info: Resolver took 2402msec
Info: Closure Simplifier took 0msec
Info: Type checker took 346msec
Info: IR builder took 0msec
Info: Type inference took 0msec
Info: ConstantHandler took 393msec
Info: Deferred Loading took 0msec
Info: Unknown task took 0msec
Info: Enqueue took 283msec
Info: Dump Info took 0msec
Info: SSA builder took 0msec
Info: SSA optimizer took 0msec
Info: SSA code generator took 0msec
Info: CodeEmitter took 0msec
Info: Total compile-time 4978msec; unaccounted 558msec

when I use pub build --mode=debug:

Loading source assets...
Building DartAgnan...
[Info from Dart2JS]:
Compiling DartAgnan|web/dart/main.dart...
[Dart2JS on DartAgnan|web/dart/main.dart]:
1 warning(s) suppressed in package:route_hierarchical.
[Info from Dart2JS]:
Took 0:00:10.001875 to compile DartAgnan|web/dart/main.dart.
Built 514 files to "build".

Do you see where is my mistake ? Or there is a debug command more explicit ?
Thank you.

Solution

I used these on server-side:

_ws.binaryType = 'arraybyte';
...
_ws.onMessage.listen((MessageEvent data) {
            var response = JSON.decode(new String.fromCharCodes(new BZip2Decoder().decodeBytes(data.data)));
            _sender_list[response['ws_id']].add(response);
            if (response['status'] != 1) {
                _sender_list[response['ws_id']].close();
            }
}

Working well without compile to JS. With compile, we get the errors indicated.

The solution is use directly Blob.

//_ws.binaryType = 'arraybyte';
...
_ws.onMessage.listen((MessageEvent data) {
  FileReader reader = new FileReader();

  reader.onLoadEnd.listen((ProgressEvent event) {
    var response = JSON.decode(new String.fromCharCodes(new BZip2Decoder().decodeBytes(reader.result)));
        _sender_list[response['ws_id']].add(response);
          if (response['status'] != 1) {
            _sender_list[response['ws_id']].close();
        }
  });
  reader.readAsArrayBuffer(data.data);
});

Now, the compilation works well.

Answered By – Druxtan

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.