External Files
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 1 of 110.
External files let you organize your classes in separate Dart files and import them into your main program.
Create a separate Dart file called my_class.dart
class MyClass {
// class body
}Import the file into your main file using the import directive
import 'my_class.dart';Now you can use the class in your main file
void main() {
MyClass obj = MyClass();
print(obj.runtimeType);
}Output:
MyClassThe import 'my_class.dart'; statement connects the my_class.dart file to your program. The filename must include the .dart extension and be wrapped in single quotes. Dart resolves the path relative to the current file.
Challenge
MediumYou are given two Dart files (my_class.dart and driver.dart). Add the correct import statement in driver.dart to connect the files so that MyClass can be used!
Cheat sheet
To use classes from external files, create separate Dart files and import them using the import directive:
Create a separate file (e.g., my_class.dart):
class MyClass {
// class body
}Import the file in your main program:
import 'my_class.dart';The filename must include the .dart extension and be wrapped in single quotes. Dart resolves the path relative to the current file.
Use the imported class:
void main() {
MyClass obj = MyClass();
print(obj.runtimeType);
}Try it yourself
// TODO: Import my_class.dart
import 'dart:io';
void main() {
String testCase = stdin.readLineSync()!;
MyClass obj = MyClass();
print('Test completed');
}
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