Creating and Publishing Dart Packages for Flutter
Tags: dart, flutter, packages, pubdev
Published: 2023-08-15 02:12:22

### 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
```

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
```

> 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
```

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: ../
...

```

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

```
import 'package:quick_button/quick_button.dart';
```

- **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 .
  ```
- **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
  ```
- **Final Publishing**: Once you've confirmed the absence of errors, publish your package to pub.dev with the following command:

  ```
  flutter pub publish
  ```
Terminal will prompt you to verify your pub.dev account.


**_Congratulations! Your Package has been published._**


![quick_button pub.dev image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7xqdnopzzsz9438d27d.jpeg)

## 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](https://github.com/pozadkey/quick_button)_**
> **_Published package: [quick_button](https://pub.dev/packages/quick_button)_**