Working with dependencies
Add Dependencies
In this step you will include a dependency on an external package. There are multiple ways to locate, install and manage dependencies. In this section you will manually add the dependency to your application to help you understand the underlying concepts.
Pub.dev
pub.dev is the official package repository for the Dart programming language and the Flutter framework. It provides a platform for developers to publish and discover Dart packages, plugins, and tools that can be used to enhance their applications. It also serves as a central source of information, including documentation and usage examples, for developers who are looking to use existing packages.
Navigate to pub.dev and search for the package english_words. Once you’ve located it, navigate to the tab labeled Installing. On that tab you’ll be presented with multiple code block sections. The first two specify commands you can run in your terminal to add the dependency. The third code block shows the line that is ultimately added to your pubspec.yaml.
At the time of this writing the package is at version 4.0.0 and the resulting entry is listed as
dependencies:
english_words: ^4.0.0
Copy the english_words: ^4.0.0 then open your pubspec.yaml in your flutter project.
Note there are two sections for dependencies in the pubspec.yaml, one listed as dependencies and another for dev_dependencies
The dev_dependencies
section of a pubspec.yaml
file is used to specify dependencies that are only needed for development, such as testing frameworks and code linters. These dependencies are not included in the final production build of the application and are only used during the development process.
By keeping these development-only dependencies separate from the main dependencies, developers can ensure that the final application only includes the necessary components and keeps its size to a minimum.
Locate the dependencies: section and paste the eglish_words dependency in the file. Be sure to adhere to proper indentation. This entry should be at the same level as the flutter:
entry.
The resulting section should include the following:
dependencies: flutter: sdk: flutter english_words: ^4.0.0
When you save the file VSCode should automatically read the file and download the package. There are cases where you may need to manually request a reload of the dependencies.
To manually load the packages listed in pubspec.yaml, run flutter pub get
from the command line.
With the dependency added, head back over to lib/main.dart to use the package in your code.
At the top of your file, reference the package by adding the following line
import 'package:english_words/english_words.dart';
Locate the MyHomePage class and add the following lines after the constructor
var words=WordPair.random();
Update the Text label to use the new variable
Text('Random Words: $words'),
The resulting code should look like the following
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({super.key});
var words = WordPair.random();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
Text('Random Words: $words'),
],
),
);
}
}
Run the updated code and see the random words displayed