Libraries & Imports
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 2 of 110.
Dart comes with a rich set of built-in libraries you can import using the import 'dart:libraryname'; syntax.
Some commonly used built-in libraries:
dart:math— mathematical functions and constants
dart:io— file, socket, and standard I/O support
dart:core— automatically imported, provides basic types
Import the dart:math library to use math functions
import 'dart:math';Use functions and constants from the library
void main() {
print(sqrt(25)); // 5.0
print(pi.toStringAsFixed(5)); // 3.14159
print(pow(2, 10)); // 1024
}Output:
5.0
3.14159
1024You can also import a file of your own alongside a built-in library. Both types of imports work the same way — the difference is whether the path starts with dart: (built-in) or a filename string (local file).
import 'dart:math'; // built-in library
import 'my_helpers.dart'; // your own fileChallenge
MediumYou are given math_tools.dart which already uses dart:math internally. Add the correct import statement in driver.dart to bring in math_tools.dart so you can call its functions!
math_tools.dart: ContainsgetSquareRoot(n)andgetPI()functions (locked)driver.dart: Add the import and the rest will work
Cheat sheet
Import built-in libraries using import 'dart:libraryname'; syntax:
import 'dart:math';Commonly used built-in libraries:
dart:math— mathematical functions and constantsdart:io— file, socket, and standard I/O supportdart:core— automatically imported, provides basic types
Example using dart:math:
import 'dart:math';
void main() {
print(sqrt(25)); // 5.0
print(pi.toStringAsFixed(5)); // 3.14159
print(pow(2, 10)); // 1024
}Import your own files using a filename string:
import 'dart:math'; // built-in library
import 'my_helpers.dart'; // your own fileTry it yourself
// TODO: Import math_tools.dart to use getSquareRoot and getPI
import 'dart:io';
void main() {
String testCase = stdin.readLineSync()!;
if (testCase == 'sqrt_test') {
print(getSquareRoot(25).toInt());
} else if (testCase == 'pi_test') {
print(getPI().toStringAsFixed(5));
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor