Using dart pigeon in a federated model

552 Views Asked by At

I"m looking at converting a dart package (https://pub.dev/packages/sounds) to a federated model using pigeon.

The documentation around combing these two pieces is a little sparse.

Looking at the video_player sample (https://github.com/flutter/plugins/tree/master/packages/video_player/video_player_platform_interface) seems to suggest a federated model as the web platform is separate.

However both the android and ios packages are part of the main package.

Is this just an historical artefact or do the ios and android packages still need to be part of the main plugin?

If they can be separated out what is the correct package(s) structure?

Are then any open source plugins that use pigeon in a fully federated model that could be used as samples?

2

There are 2 best solutions below

0
On BEST ANSWER

Having the iOS and Android projects inside of the main package is the standard as of now. I looks like there's discussion around generating federated plugins when running flutter create.

There is an article on the url_launcher federated plugin model refactor which is helpful for figuring out the FS structure:

https://medium.com/flutter/how-to-write-a-flutter-web-plugin-part-2-afdddb69ece6

https://github.com/flutter/plugins/tree/master/packages/url_launcher

As far as Pigeon is concerned, because Pigeon is in pre-release and the federated plugin design is fairly new, I doubt there are any fully federated open source packages using Pigeon. However it seems like your message spec would be owned by the my_plugin_platform_interface package and the generated code would be copied over to your platform specific package e.g. my_plugin_ios, my_plugin_android, my_plugin_macos, etc.

Hope that's of some use.

0
On

Why is video_player's android and iOS code not federated?

Is this just an historical artefact or do the ios and android packages still need to be part of the main plugin?

It appears to either be:

  • a convenience: flutter create will create a package easily with android/ios boilerplate
  • historical artefact: They might have created the package before federated plugins were released. (This is why people have requested the flutter create plugin template to generated a federated plugin automatically).

Correct package structure

If they can be separated out what is the correct package(s) structure?

They can be separated out.

For pigeon specifically, you can put the Dart generated code (from Pigeon) in your Platform specific interface, and your Native platform generated code (from Pigeon) in your platform specific package.

In your platform specific package, you should declare it to be a federated plugin (see docs and url_launcher), where url_launcher is the app facing package in that example:

flutter:
  plugin:
    implements: url_launcher
    platforms:
      macos:
        pluginClass: UrlLauncherPlugin
        fileName: url_launcher_macos.dart

Example package

If you take a look at url_launcher, all platforms have federated plugin packages.

enter image description here

Why federate

The advantage of creating a separate platform specific package is that a user can override the default implementation. For video_player, a user cannot override Android or iOS implementation because it is in the app-facing package.

More reading

After writing the above, I found this GitHub issue which goes over a lot of our concerns: https://github.com/woodemi/quick_flutter.brick/issues/22