Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart Imports


Share

Dart Imports

A library is a collection of ready-made code, like functions, classes and constants.

Dart comes with many libraries, for example for math, dates, files and JSON.

To use a library, you import it at the top of your file with the import keyword.

The core libraries of Dart all start with dart:, like dart:math.


Import a Library

The dart:math library has math functions and constants, like sqrt() (square root) and pi.

After the import, you can use all the names from the library directly:

Example

import 'dart:math';

void main() {
  print(sqrt(16));
  print(pi);
  print(max(10, 25));
}
Try it Yourself »

Result:

4.0
3.141592653589793
25

If you forget the import, Dart does not know the names, and the program does not run:

Example

void main() {
  print(sqrt(16));
}
Try it Yourself »

Result:

main.dart:2:9: Error: Method not found: 'sqrt'.
  print(sqrt(16));
        ^^^^

Note: The import statements must come before any other code in the file.



The dart:core Library

You have used print(), String, int and List without any import.

That is because they come from dart:core, which Dart imports into every file for you.

Here is a list of some of the core libraries:

LibraryWhat it contains
dart:coreNumbers, strings, lists, maps, print() and more (imported automatically)
dart:asyncTools for async code, like StreamController and Timer
dart:mathMath functions, constants and random numbers
dart:convertConvert data to and from JSON and other formats
dart:ioFiles, folders, user input and network (not in web apps)
dart:isolateRun code in isolates
dart:collectionMore collection types, like Queue

You can import more than one library in the same file:

Example

import 'dart:convert';
import 'dart:math';

void main() {
  print(jsonEncode({'name': 'Anna', 'age': 31}));
  print(min(4, 9));
}
Try it Yourself »

Result:

{"name":"Anna","age":31}
4

Import Only Some Names With show

If you only need a few names from a library, use show.

Only the names after show are imported:

Example

import 'dart:math' show max, min;

void main() {
  List<int> scores = [72, 95, 64];
  print(max(scores[0], scores[1]));
  print(min(scores[1], scores[2]));
}
Try it Yourself »

Result:

95
64

Leave Out Names With hide

hide does the opposite of show.

It imports every name from the library, except the names after hide:

Example

import 'dart:math' hide Random;

void main() {
  print(sqrt(81));
}
Try it Yourself »

Result:

9.0

Now Random is not imported, so this program does not run:

Example

import 'dart:math' hide Random;

void main() {
  print(Random(1).nextInt(10));
}
Try it Yourself »

Result:

main.dart:4:9: Error: Method not found: 'Random'.
  print(Random(1).nextInt(10));
        ^^^^^^

show and hide are useful when two libraries use the same name for different things, and you only want one of them.


Use a Prefix With as

With as, you give a library a short name, called a prefix.

Then you write the prefix and a dot before each name from the library:

Example

import 'dart:math' as math;

void main() {
  double radius = 2;
  double area = math.pi * radius * radius;
  print('Area: ${area.toStringAsFixed(2)}');
  print(math.sqrt(49));
}
Try it Yourself »

Result:

Area: 12.57
7.0

A prefix makes it clear where each name comes from.

It also stops two names from different libraries from mixing up.

Import Your Own Files

You can also put your own code in a separate file, and import it with the file name in quotes.

For example, a file called greetings.dart could have this function:

greetings.dart

String greet(String name) {
  return 'Hello, $name!';
}

In main.dart, in the same folder, you import it like this:

main.dart

import 'greetings.dart';

void main() {
  print(greet('Mia'));
}

Every Dart file is a library, so your own files work the same way as dart:math, with show, hide and as.

To use code that other people have written, you use packages, which you will learn about in the next chapter.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.