mojo 0.4.28

  • Installing
  • Versions
  • 6

Dart Mojo Applications

Mojo Application API


Application Packaging

All Dart sources for a Mojo application are collected in a specially formatted snapshot file, which is understood by Dart's content handler in the Mojo shell. This section describes what the various parts of that package are, and how they all make it to the right place.

GN Template

Dart Mojo applications are built with the GN template 'dart_pkg' defined in //mojo/public/dart/rules.gni. Here is an example:

dart_pkg("foo") {
  app_name_override = "dart_foo"
  app = "lib/main.dart"
  sources = [
  deps = [

mojom("foo_mojom") {
  sources = [

There are several parts. See the documentation in //mojo/public/dart/rules.gni for all the details.

pub packages

Dart Mojo applications may use packages from the pub package repository at

The "foo" example above has uses_pub set to true. Suppose the "foo" package's pubspec.yaml is as follows:

name: foo
version: 0.0.1
description: Foo
  crypto: ">=0.9.0 <0.10.0"

The script //mojo/public/tools/git/ should be run before build time, e.g. as a "runhooks" action during gclient sync. The script traverses a directory tree looking for pubspec.yaml files. On finding one, in the containing directory, it runs pub get. This creates a "packages/" directory in the source tree adjacent to the pubspec.yaml file containing the downloaded Dart packages. pub get also creates a pubspec.lock file that locks down pub packages to specific versions. This pubspec.lock file must be checked in in order to have hermetic builds.

During the build, The dart_pkg rule looks for a "packages/" directory, and ensures that its contents are available when running the application.

Generated bindings

The script //mojo/public/tools/bindings/generators/ and the templates under //mojo/public/tools/bindings/generators/dart_templates govern how .mojom files are compiled into Dart code.

Consider the foo.mojom file used by our example:

module foo;

struct Foo {
  int32 code;
  string? description;

This contents of this file are in the foo module. The Dart source generated for this file will end up under, e.g. //out/Debug/gen/dart- pkg/foo/lib/foo/network_error.mojom.dart, along with the other Dart sources generated for .mojom files with the "foo" DartPackage annotation in the foo module.

Resulting file

The dart_pkg rule has two results. The first result is a Dart snapshot file zipped up into a .mojo file in the build output directory---something like //out/Release/foo.mojo. This file is understood by the Dart content handler and is suitable for deployment. The second result is a directory layout of the "foo" app that can be served by a webserver. When the URL of lib/main.dart is given to the mojo_shell, the app will be run in the Dart content handler.

They layout for our "foo" example will be the following:

//packages/crypto/...  # Dart's crypto pub package.
//packages/mojo/...  # Mojo SDK Dart libraries.

Where //packages/mojo contains Dart's Mojo bindings, //packages/crypto contains the crypto pub package, and //lib/foo/ contains the bindings generated for foo.mojom.

Mojo's Dart content handler sets the package root for a Dart application to be the packages directory. Therefore, Dart sources in this application can use the following imports:

import 'package:crypto/crypto.dart';
import 'package:foo/foo/foo.mojom.dart';
import 'package:mojo/application.dart';


MOJO_SDK: 56ddb774e386f2e021b1ac58965db050b0e5353c


MOJO_SDK: 9397aa588d55e96077c3859d4a136e4e6adf57ea


MOJO_SDK: 38c5dbd1426e105d42d6b070b8e4089c9db957eb


MOJO_SDK: ee5b33cba0face525d425d6bd8682e5de038ad73


MOJO_SDK: ff47297bfaed2660b106a67d6693343b0bc8356b


MOJO_SDK: ef5cd660ec6f1c50eaff62c81ea7662662d04e15


MOJO_SDK: f56fd59ec51e1ecdd52fb848ceff1b6b6fe7f7fa


MOJO_SDK: c200560ac45a79b61b8e8c0444af471789f5669f


MOJO_SDK: 705723c25a51b30a89b6431a6377b09e4069561e


MOJO_SDK: 7c72e53d105c346dd82cbdbeb12d9e3f0712093d


MOJO_SDK: 31c5037ab2c63649632c9f2d4227ebe6f22015a4


MOJO_SDK: 1484e831758d05c7af0f27c199d24e3bbdc6383f


MOJO_SDK: 1308cd12434c9536a28592ff09399a57ef7f4f77


MOJO_SDK: c9e15130499cc859050668ea6f78485001c96251


MOJO_SDK: b3aaccc8d9408bc645ee633e7030c605f333be95


MOJO_SDK: 2ae2bccb82f37947c69ab5d6e06c2d109690a394


MOJO_SDK: ebf435275996a69482ea342aa3ae881aecd6c90e


MOJO_SDK: 3f50c60f8af5bb485f248ceea2407a59f09caad5






























  • Update version to match sky package. 0.0.7 was skipped.


  • Fix interface name capitalization bug in Dart bindings
  • Add support for interface control messages (queryVersion, requireVersion)

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  mojo: ^0.4.28

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:mojo/application.dart';
import 'package:mojo/bindings.dart';
import 'package:mojo/core.dart';
Version Uploaded Documentation Archive
0.4.28 Aug 17, 2016 Go to the documentation of mojo 0.4.28 Download mojo 0.4.28 archive
0.4.27 Jul 1, 2016 Go to the documentation of mojo 0.4.27 Download mojo 0.4.27 archive
0.4.26 Jun 28, 2016 Go to the documentation of mojo 0.4.26 Download mojo 0.4.26 archive
0.4.25 Jun 13, 2016 Go to the documentation of mojo 0.4.25 Download mojo 0.4.25 archive
0.4.24 Jun 2, 2016 Go to the documentation of mojo 0.4.24 Download mojo 0.4.24 archive
0.4.23 May 23, 2016 Go to the documentation of mojo 0.4.23 Download mojo 0.4.23 archive
0.4.22 May 18, 2016 Go to the documentation of mojo 0.4.22 Download mojo 0.4.22 archive
0.4.21 May 13, 2016 Go to the documentation of mojo 0.4.21 Download mojo 0.4.21 archive
0.4.20 May 6, 2016 Go to the documentation of mojo 0.4.20 Download mojo 0.4.20 archive
0.4.19 Apr 7, 2016 Go to the documentation of mojo 0.4.19 Download mojo 0.4.19 archive

All 34 versions...

Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Health issues and suggestions

Fix lib/src/codec.dart. (-100 points)

Analysis of lib/src/codec.dart failed with 35 errors, including:

line 73 col 44: Undefined name 'Endianness'.

line 152 col 54: Undefined name 'Endianness'.

line 158 col 53: Undefined name 'Endianness'.

line 162 col 54: Undefined name 'Endianness'.

line 168 col 53: Undefined name 'Endianness'.

Fix lib/mojo/bindings/types/service_describer.mojom.dart. (-57.81 points)

Analysis of lib/mojo/bindings/types/service_describer.mojom.dart failed with 3 errors:

line 836 col 60: The getter 'ROOT' isn't defined for the class 'Zone'.

line 861 col 60: The getter 'ROOT' isn't defined for the class 'Zone'.

line 885 col 60: The getter 'ROOT' isn't defined for the class 'Zone'.

Fix lib/src/service_describer.dart. (-25 points)

Analysis of lib/src/service_describer.dart failed with 1 error:

line 16 col 3: '_ServiceDescriberImpl.describeService' ('(String, ServiceDescriptionStub) → void') isn't a valid override of 'ServiceDescriber.describeService' ('(String, ServiceDescriptionInterfaceRequest) → void').

Fix additional 21 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/mojo/application.mojom.dart (Run dartfmt to format lib/mojo/application.mojom.dart.)
  • lib/mojo/application_connector.mojom.dart (Run dartfmt to format lib/mojo/application_connector.mojom.dart.)
  • lib/mojo/bindings/types/mojom_files.mojom.dart (Run dartfmt to format lib/mojo/bindings/types/mojom_files.mojom.dart.)
  • lib/mojo/bindings/types/mojom_types.mojom.dart (Run dartfmt to format lib/mojo/bindings/types/mojom_types.mojom.dart.)
  • lib/mojo/http_header.mojom.dart (Run dartfmt to format lib/mojo/http_header.mojom.dart.)
  • lib/mojo/interface_control_messages.mojom.dart (Run dartfmt to format lib/mojo/interface_control_messages.mojom.dart.)
  • lib/mojo/network_error.mojom.dart (Run dartfmt to format lib/mojo/network_error.mojom.dart.)
  • lib/mojo/service_provider.mojom.dart (Run dartfmt to format lib/mojo/service_provider.mojom.dart.)
  • lib/mojo/shell.mojom.dart (Run dartfmt to format lib/mojo/shell.mojom.dart.)
  • lib/mojo/url_request.mojom.dart (Run dartfmt to format lib/mojo/url_request.mojom.dart.)
  • lib/mojo/url_response.mojom.dart (Run dartfmt to format lib/mojo/url_response.mojom.dart.)
  • lib/src/application.dart (Run dartfmt to format lib/src/application.dart.)
  • lib/src/application_connection.dart (Run dartfmt to format lib/src/application_connection.dart.)
  • lib/src/buffer.dart (Run dartfmt to format lib/src/buffer.dart.)
  • lib/src/event_handler.dart (Run dartfmt to format lib/src/event_handler.dart.)
  • lib/src/event_subscription.dart (Run dartfmt to format lib/src/event_subscription.dart.)
  • lib/src/handle.dart (Run dartfmt to format lib/src/handle.dart.)
  • lib/src/internal.dart (Run dartfmt to format lib/src/internal.dart.)
  • lib/src/proxy.dart (Run dartfmt to format lib/src/proxy.dart.)
  • lib/src/struct.dart (Run dartfmt to format lib/src/struct.dart.)
  • lib/src/stub.dart (Run dartfmt to format lib/src/stub.dart.)

Maintenance issues and suggestions

Add SDK constraint in pubspec.yaml. (-50 points)

For information about setting SDK constraint, please see

Fix platform conflicts. (-20 points)

Low code quality prevents platform classification.

Package is too old. (-100 points)

The package was released more than two years ago.

Homepage URL does not exists. (-20 points)

We were unable to access at the time of the analysis.

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use mojo.dart.