0

On main.dart I get response from api username and set `String myUsername = 'stackoverflow';

How on all page use myUsername as global variable?

Pointer
  • 2,123
  • 3
  • 32
  • 59

2 Answers2

1

You can create a Singleton like this:

class SingletonTest{
  String myUsername;

  SingletonTest._private();

  static SingletonTest _instance = SingletonTest._private();
  static SingletonTest get instance => _instance;
}

After that, you can access it from anywhere in your code using:

var singl = SingletonTest.instance;

For more examples refer to:

How to build a Singleton in dart

Or you can use Provider Pattern, more info about it here:

Provider Pattern

t00n
  • 395
  • 5
  • 12
1

the answer above is right , however if you don't want to make a singleton class, all you can do is, save it to shared preference, using the shared_preferences

myPreference.setString("userName", Your user name)

and get it using

myPreference.getString("userName")

Vaidehee Vala
  • 324
  • 3
  • 7