Global Variables in Dart

Just create a library file and create fields for globals you need there. Import this library everywhere you need access to these fields.


import 'globals.dart' as globals;

main() {
  globals.isLoggedIn = true;


import 'globals.dart' as globals;

class MyComponent {
  view() {
    if(globals.isLoggedIn) {
    else {


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)

Leave a Comment