Skip to content

Instantly share code, notes, and snippets.

@nythrox
Last active January 26, 2024 06:21
Show Gist options
  • Save nythrox/d6419357789814f85731286e263ef3fc to your computer and use it in GitHub Desktop.
Save nythrox/d6419357789814f85731286e263ef3fc to your computer and use it in GitHub Desktop.
MobX dart extensions to avoid build_runner
void main() {
mainContext.config = mainContext.config.clone(writePolicy: ReactiveWritePolicy.never);
runApp(MyApp());
}
class TestCounterStore {
// ObservableInt
final count = 0.obs;
// ObservableString
final name = "olá".obs;
int get double => count * 2;
void increment() {
count.value++;
name.value = "olá " * count.value;
}
}
final store = TestCounterStore();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Obs(() => Text('Hi ${store.name.value}, You have pushed the button this many times:')),
Obs(() => Text('${store.count.value}')),
Obs(() => Text('${store.double}')),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: store.increment,
tooltip: 'Increment',
child: Icon(Icons.add),
),
),
);
}
}
extension ObsValue<T> on T {
Observable<T> get obs => Observable(this);
}
typedef Obs = Observer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment