Search code examples
dartdart-async

How can I execute two dart code in one HTML


I'm trying to build an Dart App.

This is the process that I would like to have.

At the first connection, the user have a loading page. During this time, he has an animation, and in background, the big dart file is downloaded (came from dart2js for dart).

Once it's over, the downloaded script is execute and the app cans start to work.

Any idea about the possibility of this process ?

Thank you. EDIT:

import "dart:async";
@lazy
import 'test.dart' as foo;

const lazy = const DeferredLibrary('test');

void main() {
  foo.init(); // Supposed to throw a NoSuchMethodError.
  lazy.load().then(onFooLoaded);
}

void onFooLoaded(_) {
  foo.init();
}

test.dart

library test;

void init() {
  print("coucou");
}

Solution

  • It's called deferred loading. Basically this feature exists a while already but I haven't used it myself yet. Because of some open issues this feature seemed of limited use. I saw a notice that several bugs or missing feature were fixed but I can't tell the current status.

    For more information see
    - https://api.dartlang.org/apidocs/channels/be/dartdoc-viewer/dart:async.DeferredLibrary
    - http://blog.sethladd.com/2013/04/lazy-load-libraries-in-dart.html
    - https://code.google.com/p/dart/issues/detail?id=10171 - Code Splitting in Dart
    - https://code.google.com/p/dart/issues/detail?id=3940
    - https://code.google.com/p/dart/issues/detail?id=9483

    Update

    I tried it and it works in Chrome (not in Dartium) with a few small changes

    test.dart

    library some_lib_name; // <== was missing
    
    void init() {
      print("coucou");
    }
    

    index.dart

    const lazy = const DeferredLibrary('some_lib_name'); // use the library name not the file name