Last updated:
0 purchases
eventified generator
Eventified #
Transform method calls to a stream of events.
Getting started #
Add these dependencies to your pubspec.yaml file:
dependencies:
eventified:
dev_dependencies:
build_runner:
eventified_generator:
copied to clipboard
Usage #
1. Declaring methods #
Start by declaring a class which defines a set of public methods, with a set of arguments. Annotate this class with the @eventified annotation.
Add a part 'example.g.dart'; declaration at the top of your file.
The return type of the methods must be void.
import 'package:eventified/eventified.dart';
part 'example.g.dart';
@eventified
abstract class Example {
void hello({
required bool world,
String? name,
});
void world(String name);
}
copied to clipboard
2. Generate event classes and the stream implementation. #
Run the build runner in your project by running this command line:
dart run build_runner build
copied to clipboard
If you are using flutter, use flutter pub run build_runner build instead.
3. Use implementation #
/// Create an instance from the `Streamed` generated implementation.
final streamed = StreamedExample();
/// Listen for events from the `stream`.
streamed.stream.listen((event) {
print(event);
});
/// Call methods to trigger corresponding events.
streamed
..hello(world: false, name: 'Jeff')
..world('john');
/// The previous calls prints this to console :
///
/// HelloExampleEvent(
/// world : false,
/// name : Jeff,
/// )
/// WorldExampleEvent(
/// name : john,
/// )
/// Closes the underlying stream.
streamed.dispose();
copied to clipboard
Advanced usage #
The generated classes and behaviours can be customized with annotations.
Custom event names #
You can set a baseEvent name, and specific event names.
@Eventified(baseEvent: 'MyEvent')
abstract class ExampleCustomEventNames {
@Event(name: 'HelloEvent')
void hello({
required bool world,
String? name,
});
@Event(name: 'WorldEvent')
void world(String name);
}
copied to clipboard
Add metadata #
You can generate a $metadata property which allows to reflects the event and its arguments at runtime. This might be particularly useful for serialization purpose.
@Eventified(metadata: true)
abstract class Example {
void hello({
required bool world,
String? name,
});
void world(String name);
}
copied to clipboard
The metadata keys can also be customized independently with Event and EventArgument annotations.
@Eventified(metadata: true)
abstract class Example {
@Event(metadata: 'Hello')
void hello({
required bool world,
@EventArgument(metadata: 'Name') String? name,
});
void world(String name);
}
copied to clipboard
Examples #
See the example project.
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.