DEV Community

Damilare Ajakaiye
Damilare Ajakaiye

Posted on • Edited on

Creating and Publishing Dart Packages for Flutter

What are Dart Packages?

Dart packages are a fundamental aspect of the Flutter framework, offering pre-built functionalities and features that streamline app development. These packages are readily available on platforms like pub.dev, which acts as a central repository for Flutter packages, enabling developers to access and share resources easily.

Why Embrace Dart Packages?

Leveraging Dart packages empowers developers to concentrate on crafting distinctive features and user experiences while benefiting from the collective expertise of the community. This not only accelerates the development process but also maintains a high standard of quality and cultivates an environment of shared knowledge and innovation.

Dart Package vs Plug-in Package

Dart packages encompass a collection of related code files and assets, often disseminated through the Dart package manager (pub.dev). This package manager facilitates dependency management and code sharing, contributing to a more efficient development workflow.

In contrast, a plug-in package is designed to enhance the capabilities of specific software, frameworks, or platforms. For instance, in the context of Flutter—a Dart framework—a plug-in package might grant access to native features or third-party services, expanding the app's functionality.

Let's Dive In: Creating a Dart Package

Step 1: Setting Up the Project

Begin by initiating a project named quick_button using the following command in your terminal:

flutter create --template=package quick_button
Enter fullscreen mode Exit fullscreen mode

This command creates a folder structure with the primary entry file located at lib/quick_button.

Step 2: Git Repository Setup

Initialize a Git repository for your project using the following commands in the terminal:

git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/pozadkey/quick_button.git
git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Ensure that you replace 'origin' with your actual remote address.

Step 3: Editing Essential Files

With your project files pushed to the GitHub repository, you can now proceed to edit the following files:

  • pubspec.yaml: This file contains information about the Flutter package and its dependencies. Make the following edits:

    • Author: Provide details about the creator and contributors in the pattern:
    FirstName LastName <email@example.com>
    Damilare Ajakaiye <hello@pozadkey.com>
    
    • Homepage: Include the URL of the package repository.
  • README.md: Craft a starter markdown file that succinctly outlines the purpose of the package.

  • CHANGELOG.md: Document changes made to the package in chronological order, including version information and explanations for each change.

  • License: Choose an open-source license and insert it in the proper section, replacing [year] and [fullname] with appropriate values.

Step 4: Developing the Flutter Widget

Create a custom button widget by editing the lib/quick_button file. This widget will serve as the foundation for the button's appearance and behavior.

Step 5: Building an Example

Generate an example project to test the package. Execute the following command in the root directory:

flutter create example
Enter fullscreen mode Exit fullscreen mode

In the example project's pubspec.yaml file, add the package dependencies required for the example project.

...
dependencies:
  flutter:
    sdk: flutter
  custom_alert_box:
    path: ../
...

Enter fullscreen mode Exit fullscreen mode

We can now import our package and use it in the example project.

import 'package:quick_button/quick_button.dart';
Enter fullscreen mode Exit fullscreen mode
  • Writing Tests: To ensure that all the widgets are responding correctly, you can create tests in the test/quick_button_test.dart file.

Step 5: Publishing the Package

  • Document the Package: Create comprehensive documentation that explains the concepts and usage of your package. Generate documentation using the following command:
  dart doc .
Enter fullscreen mode Exit fullscreen mode
  • Push Changes to GitHub: Ensure all changes are committed and pushed to your GitHub repository.

  • Test-Publish the Package: To verify everything is in order, perform a test-publish of your package using the command:

  flutter pub publish --dry-run
Enter fullscreen mode Exit fullscreen mode
  • Final Publishing: Once you've confirmed the absence of errors, publish your package to pub.dev with the following command:
  flutter pub publish
Enter fullscreen mode Exit fullscreen mode

Terminal will prompt you to verify your pub.dev account.

Congratulations! Your Package has been published.

quick_button pub.dev image

Step 6: Maintaining the Package

Before updating the package, remember to:

  • Push all changes to your GitHub repository.
  • Record changes in the CHANGELOG.md file.
  • Update the version in the pubspec.yaml file

Package source code: quick_button
Published package: quick_button

Top comments (0)