Last active
January 26, 2024 06:21
-
-
Save nythrox/d6419357789814f85731286e263ef3fc to your computer and use it in GitHub Desktop.
MobX dart extensions to avoid build_runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
), | |
), | |
); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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