global variables dart code example
Example: global variable flutter
Just create a library file and create fields for globals you need there. Import this library everywhere you need access to these fields.
app.dart
import 'globals.dart' as globals;
main() {
globals.isLoggedIn = true;
}
component1.dart
import 'globals.dart' as globals;
class MyComponent {
view() {
if(globals.isLoggedIn) {
doSomething();
else {
doSomethingElse();
}
}
}
globals.dart
library my_prj.globals;
bool isLoggedIn = false;
You can also
create a singleton in the globals library (see How do you build a Singleton in Dart? for more details).
use observable to get notified about changes (see Implement an Observer pattern in Dart, How can i trigger a kind of onChange event in a class for more details)