flutter_plugin_generator 0.0.9

  • Readme
  • Changelog
  • Example
  • Installing
  • 46

flutter_plugin_generator is part of the project plugin_gen.flutter and holds the annotations that should be placed in the dev_dependencies block of your pubspec.yaml

This package is responsible for generating the plugin code for you. It works together with flutter_plugin_annotations, you can read more about the annotations in the project README.

In the code below we have a simple plugin declaration and the generated code.

part 'platform_plugin.g.dart';

@MethodChannelFutures(channelName: "my channel name")
abstract class PlatformPlugin {
  Future<String> get platform;
  factory PlatformPlugin() {
    return _$PlatformPlugin();

will generate platform_plugin.g.dart :

 part of 'platform_plugin.dart';

 class _$PlatformPlugin extends PlatformPlugin {
   final MethodChannel _methodChannel = const MethodChannel('platform_plugin');

   _$PlatformPlugin() : super();

   Future<String> get platformVersion async {
     final result = await _methodChannel.invokeMethod<String>('platformVersion');

     return result;

The sample above may look silly, but it can save a lot of code when dealing with parameters and return types. Refer to the example folder, there you will find a plugin project with the generated code for more complex usages.


Why ? #

Because it is a mindless job to write this kind of code. Lazy developer rule #1 : Write once by hand, automate, have a beer.

What plugin_gen.flutter can do for you? #

It can use an abstract class and its methods/getters to generate a concrete implementation for your plugin. This project tries to find a good compromise between free modeling of your plugins and patterns that enables code generation while keeping the code simple and clean to read.

Can I create EventChannel streams? #

Yes, you can! Annotate a getter of type Stream<T> with EventChannelStream, give the annotation a channelName and run the build runner command.


part 'platform_plugin.g.dart';

abstract class PlatformPlugin {
  @EventChannelStream(channelName: 'my event channel')
  Stream<String> get platform;

  factory PlatformPlugin() {
    return _$PlatformPlugin();

Why MethodChannelFutures is applied to a class and EventChannelStream to a getter?

It is a common pattern to have multiple methods writing and reading from the same MethodChannel, but not the same can be said to an EventChannel stream.

What are the restrictions? #

Plugin class #

  • Plugin class should be abstract and have the annotation @FlutterPlugin() applied.
  • Only one MethodChannel per class or one instance if path replacements are used. Read More

Models (for return and for parameters) #

Your models will need to have a factory fromJson(Map<String, dynamic> map) and a method Map<String, dynamic> toJson(). They will be used in the serialization/deserialization of your data.

Something like :

class MyData {
  final String data;


  Map<String, dynamic> toJson() {
    return <String, dynamic>{'data': data};

  factory MyData.fromJson(Map<String, dynamic> map) {
    return MyData(data: map['data']);

What are the supported data types? #

Sample of allowed data types:













Map<String, int>

Map<String, MyData>

Map<MyData, MyOtherData>

Map<Map<List<Map<MyData, MyOtherData>>, MyOtherData>, Map<MyOtherData, MyData>> // Tested!

Classes with generics are NOT supported


PRs are welcome!


  • Added support to serialize/deserialize enums and Sets.
  • Included header in the generated code.
  • Added support to @OnMethodCall annotation.

Breaking changes

  • MethodChannels are no longer static in any case.
  • The abstract plugin class will need to have a constructor, it can be as simple like PlatformPlugin();, but we need it.
  • Fields are no longer supported, always use getters instead.


  • Serialization and deserialization of pretty much any combination of Lists, Maps and Classes (without generics).


  • Removed example apk.


  • Updated analyzer to 0.37.1.

  • Improved generated code formatting.

  • Updated documentation.

  • Included example.

  • Breaking Change

    • New @FlutterPlugin() annotation should be placed at every plugin class, it is obligatory to write a plugin.
    • MethodCallPlugin was renamed to MethodChannelFutures, this annotation is no longer obligatory to write a plugin. This means you can have a plugin with only EventChannels.
    • Having a declared Stream<T> field without an annotation EventChannelStream no longer raise an exception. After thinking about flexibility I reached the conclusion that it could limit plugin developments.


  • Added support to EventChannel using EventChannelStream annotation in fields and getters.
  • Added support to use fields and getters for MethodChannel calls.
  • Included README.md.
  • Updated documentation.


  • Added support to filter which platforms the plugin has support.
  • Renamed build.dart to flutter_plugin_generator.dart to conform with pub.dev warning.
  • Breaking change Now the generated plugin will have a static const MethodChannel shared across instances, unless at least one path replacement is the in the [MethodCallPlugin.channelName] string.


  • Removed Flutter dependency.
  • Added the ability to list in which platforms a given method will work.
  • Better type matching for Map inner types.


  • Downgrade meta lib version.


  • Improved type matching for maps and lists.
  • Included some documentation.


  • Initial release.


flutter_gen_sample_plugin #

A new Flutter plugin.

Getting Started #

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  flutter_plugin_generator: ^0.0.9

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:flutter_plugin_generator/flutter_plugin_generator.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 21, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/flutter_plugin_generator.dart. (-0.50 points)

Analysis of lib/src/flutter_plugin_generator.dart reported 1 hint:

line 297 col 17: 'isRequired' is deprecated and shouldn't be used.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (analyzer).

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
analyzer >=0.37.1 <0.38.0 0.37.1+1 0.39.4
build ^1.1.5 1.2.2
flutter_plugin_annotations ^0.0.9 0.0.9
meta ^1.1.6 1.1.8
source_gen ^0.9.4 0.9.4+6 0.9.4+7
Transitive dependencies
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.2.10 1.3.3
front_end 0.1.21+1 0.1.29
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.21+1 0.3.29
logging 0.11.4
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.2
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.6.5
build_test ^0.10.8
test ^1.6.5