Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an Example Project to a Flutter Package?

Tags:

Is there a way to autogenerate template code for the example map structure in a package?

Most packages have a /example folder within the package root that showcases the package functionality. I'm not sure what's the "best" way to create the example is, or if it even matter.

Do I create all files individually? Do I create a new project and then copy it to the package root? Can I autogenerate an example project directly in the project?

It's my first attempt on creating a package and I want to get it right the first time.

like image 835
Joel Broström Avatar asked Jun 08 '19 12:06

Joel Broström


People also ask

How do I import a project to flutter?

Step 1: Open the Android Studio and select Tools from the menu bar and click on SDK Manager. Step 2: In the newly open window click on the plugins and in the search bar search for Flutter and Dart and then install it. Step 4: Now after installing Flutter and Dart we are ready to import a Flutter project.


2 Answers

To create a Flutter package with an Example in Android Studio

  1. Create a new Flutter Project enter image description here
  2. Select Flutter Package enter image description here
  3. After the new project opens in Android Studio, select the "Terminal" tab and then run:

flutter create example

enter image description here

  1. Select "Edit Configuration"-> add configuration -> Select the main.dart file that is located in the example/lib folder enter image description here enter image description here enter image description here enter image description here

  2. Open your example pubspec.yaml and link to the library by adding:

    your_package_name: path: ..\ enter image description here

NOTE: When you open the Example folder it will look like an entire Flutter app nested into your project; that's because it is. Just open the io and android folders to see that they share that pattern: enter image description here

like image 142
Chris Sprague Avatar answered Oct 01 '22 11:10

Chris Sprague


Create a new project inside the root directory of your project called example and then remove unnecessary files such as CHANGELOG, LICENSE and README as they will be in your package folder.

Here's an example (no pun intended) with a package that I created:

enter image description here enter image description here

Now in pubspec.yaml you should include your package as dev dependency like this:

dev_dependencies:   your_package:     path: ../ 

I believe you can also use regular dependencies, but this worked for me.

Now inside /lib you can add a main.dart file, import your package and then create an example project.

like image 45
Noodles Avatar answered Oct 01 '22 09:10

Noodles