flutter_plugin_generator 0.0.8 copy "flutter_plugin_generator: ^0.0.8" to clipboard
flutter_plugin_generator: ^0.0.8 copied to clipboard

outdated

Flutter Plugin generator generates a plugin implementation for you.

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 {
  Future<String> get platform;
  
  static PlatformPlugin create() {
    return _$PlatformPlugin();
  }
}

will generate platform_plugin.g.dart :

part of 'platform_plugin.dart';

class _$PlatformPlugin extends PlatformPlugin {
  static const MethodChannel _methodChannel =
      const MethodChannel('my channel channel');

  _$PlatformPlugin();

  @override
  Future<String> get platform async {
    
    final result = await _methodChannel.invokeMethod<String>('platform');
    
    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/fields/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 field 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 {
  
  @EventChannelStream(channelName: 'my event channel')
  Stream<String> get platform;

  static PlatformPlugin create() {
    return _$PlatformPlugin();
  }
}

Why MethodChannelFutures is applied to a class and EventChannelStream to a field/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
  • A factory for the concrete implementation is not allowed at the moment.

NOTE: Use an static method in the abstract plugin class to encapsulate the instantiation.

  static PlatformPlugin create() {
    return _$PlatformPlugin();
  }

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

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!

1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Flutter Plugin generator generates a plugin implementation for you.

Repository (GitHub)
View/report issues
Contributing

License

MIT (LICENSE)

Dependencies

analyzer, build, flutter_plugin_annotations, meta, source_gen

More

Packages that depend on flutter_plugin_generator