Skip to content

Instantly share code, notes, and snippets.

@shailen
Created September 28, 2014 01:26
Show Gist options
  • Save shailen/f4a3d8c8e01229547eb9 to your computer and use it in GitHub Desktop.
Save shailen/f4a3d8c8e01229547eb9 to your computer and use it in GitHub Desktop.

Dart Style Guide

Author: Shailen Tuli Reviewers: Trevor Johns, Kathy Walrath

Existing guide

All sample code must comply with the official Dart Style Guide.

Tools

Use dartfmt to format your Dart code.

Additional guidelines for Dart code

These guidelines are not required by the official Dart Style Guide, but must be followed in your Dart code. This includes samples (including snippets added to docs), larger apps, and libraries.

Import Order

Imports should be ordered as follows, with each group ordered alphabetically. Leave a blank line after each group.

  • dart: imports
  • package: imports
  • relative imports

For example:

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

import 'package:polymer/polymer.dart';

import 'src/header.dart';
import 'src/navbar.dart';

Comments

  • Do not use block (/** ... */) comments.
  • Use // for comments.
  • Use /// if you're showing a doc comment.

Defer to the Guidelines for Dart Doc Comments for how to format comment text.

Directory structure

Use the Pub package layout conventions to organize your files and directories.

When writing short samples, it is acceptable to put all your files in web (for web applications), bin (for executables), and lib (for library code). You can also put samples in example.

Library declarations

Add a library declaration to each .dart file. Prefix library names with the package name and a dot-separated path. For example:

// In lib/foo/bar.dart
library my_package.foo.bar;

// In example/foo/bar.dart
library my_package.example.foo.bar;

See the Dart Style Guide for details.

Testing

Use the Dart unittest package. Read the Unit Testing with Dart article for guidelines on how to use this package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment