How to Import a Library in Dart

In Dart, libraries are a way to organize and encapsulate code. You can import libraries to use their functionality in your Dart programs. Dart provides a simple syntax for importing libraries, allowing you to access classes, functions, and variables defined in other files or packages.

1. Importing a Library from a File

To import a library from a local file, you use the import keyword followed by the relative or absolute path to the Dart file you want to import. The path should be enclosed in single or double quotes.

Example of Importing a Local Library

// file: math_utils.dart
int add(int a, int b) {
return a + b;
}

// file: main.dart
import 'math_utils.dart';

void main() {
int result = add(3, 5);
print('The sum is: $result'); // Output: The sum is: 8
}

In this example, we define a simple utility function add in a file named math_utils.dart. We then import this file in main.dart using the import statement. This allows us to call the add function in the main function.

2. Importing a Library from a Package

Dart also allows you to import libraries from packages. Packages are collections of Dart files that can be shared and reused. To import a package, you use the import keyword followed by the package name and the path to the library within the package.

Example of Importing a Package

// Importing the 'http' package
import 'package:http/http.dart' as http;

void main() async {
// Making a GET request
var response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts'));
print('Response status: ${response.statusCode}');
}

In this example, we import the http package, which is commonly used for making HTTP requests. We use the as keyword to create an alias for the package, allowing us to reference its classes and functions with the http. prefix. We then make a GET request to a sample API and print the response status.

3. Importing Only Specific Parts of a Library

If you only need specific classes or functions from a library, you can use the show keyword to import only those parts. This can help reduce the amount of code you need to load and improve readability.

Example of Importing Specific Parts

library math_utils;

int add(int a, int b) {
return a + b;
}

int subtract(int a, int b) {
return a - b;
}

// file: main.dart
import 'math_utils.dart' show add;

void main() {
int result = add(10, 5);
print('The sum is: $result'); // Output: The sum is: 15
}

In this example, we import only the add function from the math_utils.dart library using the show keyword. This allows us to use the add function without importing the entire library.

4. Importing All Parts of a Library

If you want to import all parts of a library but want to avoid naming conflicts, you can use the as keyword to create an alias for the library.

Example of Importing All Parts with an Alias

import 'math_utils.dart' as math;

void main() {
int sum = math.add(5, 10);
print('The sum is: $sum'); // Output: The sum is: 15
}

In this example, we import the entire math_utils.dart library and create an alias math. This allows us to call the add function using the math.add syntax, avoiding any potential naming conflicts with other libraries.

5. Conclusion

Importing libraries in Dart is a straightforward process that allows you to organize and reuse your code effectively. By using the import statement, you can access functions, classes, and variables from other Dart files or packages, enhancing the modularity and maintainability of your applications. Whether importing local files or external packages, Dart provides flexible options to suit your needs.