I am creating an app that logs in users and saves the user's data locally. The problem is it displays null when I try to display it on a text field or something but when I print said variable inside the function, it displays the value. I'm not sure what the problem is here.
Here's the code.
String? email = '';
void getEmail() async {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
SharedPreferences prefs = await _prefs;
setState(() {
email = prefs.getString('username');
});
}
So the 'email' variable is what keeps returning null, as you can see in the function, when the 'getEmail' is called, it should reassign the 'email' variable to the acquired value.
When I try to display it, it returns null or nothing at all, but when I call a print in the fuction, it returns the true value, what is the problem here?
Example
void getEmail() async{
...
email = prefs.getString('username');
print(email);
the result of this would be 'jafar'
but if I call it in a Text or whatnot in the build Method, For example
TextButton(onPressed: (){getEmail();
print(email);}, child: Text('click')),
Text('${email}')
everything returns null; what is the problem?
Complete Code
class Example extends StatefulWidget {
const Example({Key? key}) : super(key: key);
State createState() => _ExampleState();
}
class _ExampleState extends State {
@override
Widget build(BuildContext context) {
FirebaseAuth auth = FirebaseAuth.instance;
String? email = '';
void getEmail() async {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
SharedPreferences prefs = await _prefs;
setState(() {
email = prefs.getString('username');
});
}
return Scaffold(
body: SafeArea(child: Column(children: [
TextButton(onPressed: (){getEmail();
print(email);}, child: Text('click')),
Text('${email}')
],),)
);
}
}

