How to publish a Flutter package

bhardwaju
Staff

In this article, we'll discuss how to publish a Flutter package. We'll use the pub command-line tool to publish the package to the Dart Pub repository.

If you have any questions, please leave a comment below! 

To get started, we need to create a new pubspec.yaml file in the root directory of our Flutter project. This file will contain the information about our package, such as its name, version, and dependencies.

The following is an example of a pubspec.yaml file:

 

name: my_package
version: 1.0.0
description: A Flutter package for doing cool things.

dependencies:
  flutter: 2.0.0

 

Once we've created the pubspec.yaml file, we need to run the pub command-line tool to publish the package. This can be done by running the following command:

 

pub publish

 

This command will publish the package to the Dart Pub repository. It will also create a new pubspec.yaml file in the .pub directory of our Flutter project. This file will contain the information about our package, such as its name, version, and dependencies.

The following is an example of a pubspec.yaml file in the .pub directory of our Flutter project:

 

name: my_package
version: 1.0.0
description: A Flutter package for doing cool things.

dependencies:
  flutter: 2.0.0

 

Now, we can use our package in our Flutter project. We can do this by adding the following code to the main.dart file: 

 

import 'package:my_package/my_package.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hello World!');
  }
}

 

This code will import our package and use it in our Flutter project.

That's it! We have successfully published a Flutter package.


Authored by:

  • Shivank Awasthi (@shivankawasthi) Strategic Cloud Engineer, Google
  • Utkarsh Bhardwaj (@bhardwaju) Cloud Migration Consultant, Google

Video by:

  • Amit Dutta (@amitdutta) Strategic Cloud Engineer, Google
3 0 1,637
Authors