A Step by Step Guide on How to Create Your Own Flutter Package and publish it on pub.dev

Amir Hoss
2 min readJul 12, 2024

--

flutter create --template=package first_flutter_package

2. Update the pubspec.yaml file:
This file contains metadata about your package, including its name, version, author, and dependencies

3. Update the README.md file with instructions on how to use your package.

4. Create a CHANGELOG.md file:
This file should contain a chronologically ordered list of notable changes for each version of your package.

5. In the lib folder, create a folder called src and inside it create a new file called first_flutter_package.dart. This file will contain the logic for arranging the three widgets based on the device screen size.

6. Update a file first_flutter_package.dart in the lib directory of the package.

library first_flutter_package;

export 'src/first_flutter_package.dart';

7. Update the test/first_flutter_package_test.dart file

import 'package:flutter_test/flutter_test.dart';

void main() {
test('adds one to input values', () {
// TODO: test
});
}

8. Now Example turn


flutter create --org com.example --platforms=android,ios,linux,macos,windows -a kotlin example
cd example

Append first_flutter_package to example

...
dependencies:
...
first_flutter_package:
path: ../

10. Run example app

$ flutter pub get
$ flutter run


r Hot reload. 🔥🔥🔥
R Hot restart.
v Open Flutter DevTools.
s Save a screenshot to flutter.png.
w Dump widget hierarchy to the console. (debugDumpApp)
t Dump rendering tree to the console. (debugDumpRenderTree)
L Dump layer tree to the console. (debugDumpLayerTree)
f Dump focus tree to the console. (debugDumpFocusTree)
S Dump accessibility tree in traversal order. (debugDumpSemantics)
U Dump accessibility tree in inverse hit test order. (debugDumpSemantics)
i Toggle widget inspector. (WidgetsApp.showWidgetInspectorOverride)
p Toggle the display of construction lines. (debugPaintSizeEnabled)
I Toggle oversized image inversion. (debugInvertOversizedImages)
o Simulate different operating systems. (defaultTargetPlatform)
b Toggle platform brightness (dark and light mode). (debugBrightnessOverride)
P Toggle performance overlay. (WidgetsApp.showPerformanceOverlay)
a Toggle timeline events for all widget build methods. (debugProfileWidgetBuilds)
M Write SkSL shaders to a unique file in the project directory.
g Run source code generators.
j Dump frame raster stats for the current frame. (Unsupported for web)
h Repeat this help message.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

9. Check and Publish package

### Optional
dart fix --dry-run

dart fix --apply

## dart fix --apply --code=prefer_const_constructors
## dart fix --apply --code=prefer_const_constructors_in_immutables
## dart fix --apply --code=unused_import




### In package folder first_flutter_package
dart pub publish --dry-run

### If no error
dart pub publish

--

--

Amir Hoss

✔Senior PHP Developer✔Mobile Developer✔MySQL Administrator [myWebsite:https://amirhome.com]