Slide 1

Slide 1 text

Developing a Flutter package Reusing code and widgets

Slide 2

Slide 2 text

Developing a Flutter package 1 2 3 4 5 6 Creating a package Path vs Git dependencies Specifying subfolders and refs Exporting symbols Micro-libraries Github actions and secrets

Slide 3

Slide 3 text

Developing a Flutter package Reusing code and widgets Founder and CEO Art Plus Code Inc. Alexandre Quessy artpluscode.com artpluscode.com

Slide 4

Slide 4 text

Creating a package flutter create flutter create --template package --project-name my_widgets --org com.example --description "My widgets" dart create dart create --template package my_functions

Slide 5

Slide 5 text

dependencies: hello_package: path: ../hello_package/ Your app can depend on a package in a local folder: Editing the pubspec.yaml dependencies: hello_package: git: url: https://github.com/artpluscodeinc/hello_package.git It can depend in a package in a remote Git repository: The official documentation: https://docs.flutter.dev/packages-and-plugins/using-packages#dependencies-on-unpublished-packages

Slide 6

Slide 6 text

dependencies: hello_package: git: url: [email protected]:artpluscodeinc/hello_package.git ref: main Can point to a specific branch or tag: (and use SSH instead of HTTPS) Editing the pubspec.yaml (cont'd) dependencies: hello_package: git: url: [email protected]:artpluscodeinc/many_packages.git path: packages/hello_package ref: 0.1.0 Can specify the path to a subfolder: (and point to a tag)

Slide 7

Slide 7 text

dependency_overrides: hello_package: path: ../hello_package/ Create another file called "pubpec_overrides.yaml" Overriding the pubspec.yaml

Slide 8

Slide 8 text

/// Our first library. library hello_package; export './src/counter_widget.dart' show CounterWidget; The code is lib/src/ is private, but can be exported from the files in lib/: Exporting symbols import 'package:hello_package/hello_package.dart' show CounterWidget; Then we can import it from our app:

Slide 9

Slide 9 text

Create multiple public files in lib/ Micro-libraries Each file can be imported separately That's it! + + import 'package:hello_package/widgets.dart' show CounterWidget; import 'package:hello_package/add_numbers.dart' show add_numbers;

Slide 10

Slide 10 text

Testing your app

Slide 11

Slide 11 text

# This workflow installs the dependencies and runs the tests on: pull_request: name: Flutter Test jobs: build: name: Run flutter test runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-java@v1 with: java-version: '12.x' - uses: subosito/flutter-action@v2 with: channel: 'stable' - uses: webfactory/[email protected] with: ssh-private-key: | ${{ secrets.SSH_PRIVATE_KEY_FOR_THIS_APP_REPO}} - name: Get dependencies run: flutter pub get - name: Run tests run: flutter test --reporter expanded Use SSH keys if your library is private Setup your Github action Encrypted secrets: https://docs.github.com/en/actions/security-guides/encrypted-secrets Managing deploy keys: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/managing-deploy-keys Create a SSH key pair Add the public key as a deploy secret for your app Add the private key as a secret for your package + + +

Slide 12

Slide 12 text

private: false To publish a package on pub.dev, first make it public: Publishing a public package dart pub publish --dry-run dart pub publish Log in to pub.dev with a Google account, and upload it: The official documentation: https://dart.dev/tools/pub/publishing Make sure its LICENSE is permissive (BSD-3) Edit the CHANGELOG.md + + Publishing is forever! You can then transfer it to a verified publisher, if you administer one. + +

Slide 13

Slide 13 text

Avenues for future investigations: Other considerations Use platform channels to write platform-specific code Use Git tags to specific some exact version of your library + + Mark deprecated symbols with the @Deprecated annotation +

Slide 14

Slide 14 text

THANK YOU! v