Open In App

How to Import Local Package in Flutter?

Last Updated : 25 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Flutter is a framework that is used for developing cross-platform applications for Android, iOS, Linux, macOS, Windows, and other web applications using a single source code and it was developed by Google. Flutter is a free and open-source software development kit for developing mobile applications. Developers use Dart Programming Language for building mobile Applications using Flutter Framework. Dart is a typed object programming language that majorly focuses on UI development and its syntax is very similar to JavaScript. To know more about flutter, you can follow the flutter tutorial.

Why Flutter?

Flutter enables us to create both android and iOS applications using a single source code and programming Language which makes the developers complete the project in less time and in an efficient way. Everything in flutter is treated as a Widget and Flutter also has a widget library that contains elements such as buttons, scaffold, text areas, and many more which can be reused.

As we see in the industry many companies are preferring the Flutter framework along with the dart programming language for developing mobile applications because of these features.

  1. Easy to learn.
  2. Cost-effective.
  3. Great performance.
  4. Increased productivity.
  5. Available on different IDEs.
  6. Great documentation & community.

And now coming into the article we are going to discuss how to import local packages in a flutter. Packages are the predefined codes in any programming language which are also called libraries. By importing them we use all the functionalities like methods and attributes in them in the host module. By this, we can reduce the lines of code.

Flutter packages are open-source libraries that can be used by anyone at no cost. It not only reduces the lines of code but also minimizes the effort of the developer and maximizes the efficiency of the project. Before going into the steps of implementation, we have to know what a package is.

The package is a set of code that would be reused at any part of our project by just importing name without rewriting all the code again and adding that functionality, it can be a set of projects imported into a large project also. Packages would be two types that are user-defined which are written in your code as separate modules and used in your other module, and library packages that should be installed from some source of internet and need to be imported. Now we are going into the detailed steps on how to import the local packages in a flutter.

Case 1:

If you want to import your module as a package into another module.

Step 1: To set up VSCode you can follow the VSCode tutorial. For example, you have a welcome screen and get started screen and when you click on the Next button on the welcome screen it should redirect to the Get started screen.

Welcome-screen

Step 2: We have to import the Get Started module as a package into the Welcome Screen.

Importing-module-as-a-package

Importing a module as a local package

Now it’s sorted out and we get all functionalities present in that imported module to the host module.

Case 2:

If we want to import a module that is predefined Library in flutter or Dart Programming Language.

Step 1: We have to install the library from the dev platform of the Flutter or Dart. By finding the library by name in Dev Platform, like https://pub.dev/packages/firebase_auth. Here firebase_auth is the library name and follow the instructions in Installing button on the page.

Searching-for-your-package-name

Search for your package name here

Step 2: Then you can find it by using a command to add the package into our code. For adding the package into our code, we have to add the library as a dependency into our pubspec.yaml file in our development environment.

By using the command.

$ flutter pub add Library name

This will directly add the dependency to the pubspec.yaml file. We can also add the dependency manually without using the above command. Open the Pubspec.yaml file and add the dependency as library name: version number as shown in the figure below.

Opening-Pubspec-yaml-file

Then it will automatically install your package into your system in either of the above two ways. Now you are pending with just importing the package into the required module. By using,

import ‘Package name’;

Or using the complete path of the file name.

import ‘package:path/package name’;

This is the process of importing Local packages into flutter in different scenarios.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads