packages flutter banner img

Nikita_Dabari_profile
Nikita DabariSoftware Engineerauthor linkedin
Published On
Updated On
Table of Content
up_arrow

In this post, I'll walk you through how to create Flutter packages to build Dart packages. Whether you're an experienced Flutter developer looking to share your knowledge or a beginner excited to contribute to the Flutter community, this post will explain how to package and share your Flutter code.

Flutter packages make it easy to share and reuse code in your projects. Whether you want to contribute to the Flutter community or make your app development process smoother, creating packages is the way to go.

In Flutter, we use Dart to build cross-platform apps. Dart packages are like libraries you’d install in Node.js projects using npm or yarn. These packages are created by Flutter developers for other Flutter developers.

Advantages of creating packages

adv-of-creating-packages

1. Reusability: Packages allow you to use the same code in different projects. This is useful for sharing common features or designs, saving you time since you don't need to rebuild the same things repeatedly.

2. Independence: Packages can be developed separately from the main app. This means you can update or replace parts of your app without affecting the entire project.

3. Easy Sharing: Teams can share packages across projects, making development faster and avoiding duplicate work.

4. Focused Work: By developing packages, teams can work on specific parts of the app without needing to handle the entire code at once.

5. Version Control: Each package can have its own version and update schedule. This is helpful when different parts of the app change at different times.

6. Easier Maintenance: If you need to fix bugs or update features, you can make changes directly in the package without impacting the rest of the app.

Now, let’s get started with a step-by-step practical guide on how to create packages for Flutter. 

Step 1: Set Up Your Environment

Ensure Flutter and Dart are installed on your system. You can verify this by running

flutter --version

If not installed, follow the official Flutter installation guide.

Step 2: Create a Flutter Package

To create a Flutter package, go to your command line interface and directory where you want to create the Flutter package and run the command mentioned below:

  flutter create --template=package my_flutter_package

Replace my_flutter_package with your desired package name. This command sets up a new package project with the necessary files.

package_name/
├── lib/
   └── my_flutter_package.dart
├── pubspec.yaml
├── README.md
└── example/    
├── lib/    
  └── …

LICENSE

This file is mostly blank and is meant for adding legal terms regarding package usage.

test/my_flutter_package_test.dart

The unit tests for the package.

my_flutter_package.iml

A configuration file used by the IntelliJ IDEs.

.gitignore

 A hidden file that tells Git what files or folders it should ignore when tracking changes in a project.

.metadata

A hidden file is used by IDEs to track the properties of the Flutter project.

Pubspec.yaml

A YAML file containing metadata that specifies the package’s dependencies. Used by the pub tool.

README.md

A markdown file that briefly describes the package’s purpose and its usage.

Step 3: Write Your Package Code

Inside the lib/ directory, create a Dart file. For example, my_flutter_package.dart. Add your package code to this file. Here’s a simple example:

library my_flutter_package;

class MyUtility {
  static String greet(String name) {
    return 'Hello, $name!';
  }
}

This creates a basic utility class with a function that greets the user.

Step 4: Update pubspec.yaml file

  • Open pubspec.yaml and update the package information, including the name, description, author, and version.
  • Add dependencies if your package requires other Flutter or Dart packages. For example
  • name: my_flutter_package
    description: A simple greeting package
    version: 0.0.1

    dependencies:
      flutter:
        sdk: flutter

    Step 5: Add Documentation

    • Update the README.md file to explain what your package does and how to use it. Providing examples makes it easier for others to understand.
    • Also, add comments in your Dart code to document your functions and classes.

    Step 6: Write Unit Tests

    • Inside the test/ folder, create a test file, e.g., my_flutter_package_test.dart.
    • Write tests to verify that your package works as expected. For example

    import 'package:flutter_test/flutter_test.dart';
    import 'package:my_flutter_package/my_flutter_package.dart';

    void main() {
      test('Greet function test', () {
        expect(MyUtility.greet('World'), 'Hello, World!');
      });
    }

    Use the flutter test command to run the tests.

    flutter test

    Step 7: Version Control with Git

    • Initialize a Git repository if you haven’t already:

    git init

    Add files to the repository and make an initial commit

    git add .
    git commit -m "Initial commit for my_flutter_package"
    • Make sure to use the .gitignore file to ignore unnecessary files.

    Step 8: Publish the Package (Optional)

    If you want to publish your package to pub.dev, follow these steps:

  • Create an account on pub.dev and verify your email.
  • Run flutter pub publish --dry-run in your package directory to ensure there are no issues.
  • If everything looks good, publish the package using
  • flutter pub publish

    Follow the instructions on the screen to complete the publication process.

    Step 9: Use Your Package in a Flutter Project

    • Add your package to the pubspec.yaml of your project
    dependencies:
      my_flutter_package:
        path: ../path_to_your_package

    Import and use the package in your Dart files

    import 'package:my_flutter_package/my_flutter_package.dart';

    void main() {
      print(MyUtility.greet('Nikita'));
    }

    Conclusion

    In conclusion, Flutter packages and plugins are powerful tools. Developers can save time and effort by reusing code across projects. This eliminates the need to rewrite the same functionality from scratch. By utilizing packages from pub.dev or creating custom packages and plugins, developers can enhance productivity, reuse code, and access native features seamlessly across different platforms.

    Schedule a call now
    Start your offshore web & mobile app team with a free consultation from our solutions engineer.

    We respect your privacy, and be assured that your data will not be shared