Upgrade to Pro — share decks privately, control downloads, hide ads and more …

July Flutter Meetup - Developing a Flutter Pack...

July Flutter Meetup - Developing a Flutter Package by Alexandre

GDG Montreal

July 13, 2023
Tweet

More Decks by GDG Montreal

Other Decks in Technology

Transcript

  1. 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
  2. Developing a Flutter package Reusing code and widgets Founder and

    CEO Art Plus Code Inc. Alexandre Quessy artpluscode.com artpluscode.com
  3. 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
  4. 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
  5. 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)
  6. /// 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:
  7. 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;
  8. # 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 + + +
  9. 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. + +
  10. 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 +