'Firebase Flutter Does Not Recognize My String as Parameter

I am trying to get user email,save to shared preferences and use as collection name in another file.

my code to save

Future<void> saveEmail() async {
var sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString("email", _emailKontroller.text);}

no problem here, I can save data to sharedPreferences and read data from another file.

my code to read

  @override
void initState() {
// TODO: implement initState
void initilaizeEmail() async {
  var sharedPreferences = await SharedPreferences.getInstance();
  _email = sharedPreferences.getString("email");
  print(_email);
}

initilaizeEmail();
setState(() {});

}

output

I/flutter ( 3274): [email protected]

where I use as parameter my sharedPreferences Data:

 query: FirebaseFirestore.instance
        .collection("test")
        .doc("$_email")
        .collection("class 0"),
    // to fetch real-time data
    isLive: false,

I can not see anything on screen but, if I delete

_email

and type "[email protected]" manually everything works.What is the problem?



Solution 1:[1]

I solved my problem with using

Future Builder

Solution 2:[2]

The problem is that initilaizeEmail is an async method, and you're not waiting for its result. To fix this:

await initilaizeEmail();

I also recommend fixing the name of the method to be initializeEmail. While it won't change the behavior, spelling mistakes tend distract from other problems.

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 cesebe27
Solution 2 Frank van Puffelen