Home > OS >  Why variable token is null?
Why variable token is null?

Time:04-15

i am writing this dart code but i've a problem with assignment value to "token".

String? token;
loadJson().then((response) {
    token = response;
});

print(token); //Print null

Thanks!

CodePudding user response:

print the token in the then function:

String? token;
loadJson().then((response) {
    token = response;
    print(token);
});

Or use await:

String? token;
var response = await loadJson();
token = response;
print(token);

CodePudding user response:

Becuase your code is kind of equal to

String? token;

print(token); //Print null
///sleeep some time
token = loadJson();
  • Related