Instance of 'Future<String>' instead of showing the value
Asked Answered
H

2

18

Iam using flutter and I am trying to get a value from shared_preferences that I had set before, and display it in a text widget. but i get Instance of Future<String> instead of the value. here is my code:

Future<String> getPhone() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    final String patientPhone = prefs.getString('patientPhone').toString();
        print(patientPhone);

    return patientPhone;
  }

Future<String> phoneOfPatient = getPhone();

Center(child: Text('${phoneOfPatient}'),))
Hix answered 27/1, 2019 at 15:33 Comment(0)
B
15

There is await missing before prefs.getString( and use setState() instead of returning the value. build() can't use await.

  String _patientPhone;

  Future<void> getPhone() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    final String patientPhone = await /*added */ prefs.getString('patientPhone');
    print(patientPhone);

    setState(() => _patientPhone = patientPhone);
  }

  build() {
    ...
    Center(child: _patientPhone != null ? Text('${_patientPhone}') : Container(),))
  }
Buonaparte answered 27/1, 2019 at 15:36 Comment(10)
I am not getting that error but the value that is shown is null and I am sure that it is not null because I am using it in another file(that file is a http response and does not have a builder)Hix
Did you use my _patientPhone != null ... code? The value will be null at first because the code to get it is async and will only result in a value != null after a while. When Flutter runs build() in the meantime, the value will still be null.Gnatcatcher
Does print(patientPhone); ever print anything?Gnatcatcher
in other files it does, but here it does not.Hix
also here with the previous codes it did show the phone, but when I changed it to ur code, it does not print anythingHix
Try without await /*added */. It seems it is not required here. Instance of 'Future<String>' seems to be coming from another part of your cod.Gnatcatcher
If there is no phone number stored under this key, then you don't get one. I can't know from the provided information.Gnatcatcher
no there is a phone, because i am using it to receive response from the server and i am getting the response, also it gets printed in other filesHix
Everything is possible but I can't verify from here and depend on information you provide. If there were a phone print(patientPhone); would print it though.Gnatcatcher
github.com/mojtabaparvar/myapp this is my code on github . you can take a look at itHix
G
7

If you don't have the option to use await or async you can do the following.

getPhone().then((value){
 print(value);
});

and then assign a variable to them. From that, you'll have the result from the value.

Gorizia answered 29/4, 2021 at 21:32 Comment(1)
Please how can i set the value, mine is only active inside the function not outside,Rhodie

© 2022 - 2024 — McMap. All rights reserved.