'How can I separate my flutter firebase/firestore implementation and providers to a independent package or library?

Currently, I am developing a flutter web app using Provider and Firebase. I would like to develop others apps (for example and admin console) with the same database and data providers.

My app currently have the following folders

my_app
  - models
  - providers
  - services (firebase/firestore crud)
  - pages
  - utils
  - widgets

I would like to have models, providers, and services as a local package or library. Something like:

my_lib
  - models
  - providers
  - services
my_app
  - pages
  - utils
  - widgets
my_other_app
  - pages
  - utils
  - widgets

First, I don't know how to create a local lib, and how to make it a dependency of my_apps.

Second, since I will be using Firebase and Firestorage on my_lib, I don't know how the instances initialized in main are used by the package. Is it enough to initiate the global variables on the my_lib?



Solution 1:[1]

In your project folder you can create a folder packages and then create your 'packagesusingflutter create --template=package my_package`,

Have your app can depend on it in pubspec.yaml

 my_package:
   path: ../packages/my_package

in this case you will have

 - my_project
    -lib
    -packages
      - my_package

This works well when its just your app depending on the packages.

If you need to access your packages in different projects you can use github/gitlab to host your packages .

Say you have two repos that need to share a private package, my_app and my_admin , you can create a new repo my_packages and have your packages in it I.E

 - my_packages
   - package1
   - package2

in your apps you can now depend on the packages using git eg

 package1:
    git:
      url: [email protected]:username/my_packges.git
      ref: main // branch
      path: ./package1 //package name
package2:
    git:
      url: [email protected]:username/my_packges.git
      ref: main // branch
      path: ./package2 //package name


For github you need ssh key

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 griffins