flutter_plugin_generator 0.0.9

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';

@FlutterPlugin()
@MethodChannelFutures(channelName: "my channel name")
abstract class PlatformPlugin {
  
  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();

   @override
   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.

FAQ #

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.

Example:

part 'platform_plugin.g.dart';

@FlutterPlugin()
abstract class PlatformPlugin {
  
  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;

  MyOtherData({this.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:

int

double

String

void

Null

MyData

MyEnum

Set<int>

List<int>

List<double>

List<String>

List<MyData>

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

MyGenericData<T>

PRs are welcome!

[0.0.9]

  • 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.

[0.0.8]

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

[0.0.7+1]

  • Removed example apk.

[0.0.7]

  • 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.

[0.0.6]

  • 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.

[0.0.5]

  • 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.

[0.0.4]

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

[0.0.3]

  • Downgrade meta lib version.

[0.0.2]

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

[0.0.1]

  • Initial release.

example/README.md

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:


dependencies:
  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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
2
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
47
Learn more about scoring.

We analyzed this package on Aug 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: other

Primary library: package:flutter_plugin_generator/flutter_plugin_generator.dart with components: io, build, mirrors.

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.

Dependencies

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.38.0
build ^1.1.5 1.1.6
flutter_plugin_annotations ^0.0.9 0.0.9
meta ^1.1.6 1.1.7
source_gen ^0.9.4 0.9.4+3
Transitive dependencies
args 1.5.2
async 2.3.0
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.2
csslib 0.16.1
dart_style 1.2.10
front_end 0.1.21+1 0.1.22
glob 1.1.7
html 0.14.0+2
kernel 0.3.21+1 0.3.22
logging 0.11.3+2
package_config 1.1.0
path 1.6.4
pedantic 1.8.0+1
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
yaml 2.1.16
Dev dependencies
build_runner ^1.6.5
build_test ^0.10.8
test ^1.6.5