'Why variable token is null?
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!
Solution 1:[1]
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);
Solution 2:[2]
Becuase your code is kind of equal to
String? token;
print(token); //Print null
///sleeep some time
token = loadJson();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MendelG |
| Solution 2 | Antoniossss |
