Skip to content

Instantly share code, notes, and snippets.

@Cretezy
Last active May 7, 2020 23:34
Show Gist options
  • Save Cretezy/4cc8d9ef15910cc868cba2997586fb59 to your computer and use it in GitHub Desktop.
Save Cretezy/4cc8d9ef15910cc868cba2997586fb59 to your computer and use it in GitHub Desktop.
import 'package:flutter_super_state/flutter_super_state.dart';
// Modules extend `StoreModule`
class CounterModule extends StoreModule {
// Read only property, to avoid accidentally setting `counter` without calling `setState`
int get counter => _counter;
var _counter = 0;
// This automatically registers your module to your store
CounterModule(Store store): super(store);
// Synchronous actions
void increment() {
setState(() {
_counter++;
});
}
void decrement() {
setState(() {
_counter--;
});
}
// Asynchronous action
Future<void> reset() async {
// Fake async delay
await Future.delayed(Duration(milliseconds: 10));
setState(() {
_counter = 0;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment