Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Dart order


May 23, 2021 Dart Code style guide


Table of contents


Order

You should declare the import statement that references the Dart library, then the statement that references the package, and finally the other related references.

Each different reference section should be separated by an empty line.

Inside each section, it should be arranged in alphabetical order. If you use package: reference statement to refer to the contents of your own package, it is best to place it in the relevant reference.

// good
import 'dart:async';
import 'dart:convert' show JSON;
import 'dart:html';

import 'package:bar/bar.dart'
import 'package:bar/foo.dart'
import 'package:foo/bar.dart'

import 'a.dart';
// bad
import 'dart:html';
import 'dart:async';
import 'dart:convert' show JSON;

import 'a.dart';
import 'package:bar/bar.dart'
import 'package:foo/bar.dart'
import 'package:bar/foo.dart'

It is best to declare its exit for all reference statements in each section.

An empty line should be available before the exit section.

// good 
import 'src/error.dart';
import 'src/string_source.dart';

export 'src/error.dart';
// bad
import 'src/error.dart';
export 'src/error.dart';

import 'src/string_source.dart';