-1

I am new in flutter.I try to learn SharedPreferences and i have this exception. How can i solve this?

class _MyAppState extends State {

 Future<SharedPreferences> prefs = SharedPreferences.getInstance(); 
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Column(
              children: <Widget>[
                RaisedButton(
                  onPressed: () {addStringToSF();},
                ),
                Text(getStringValuesSF()),
              ],
            ),


        );
      }

      addStringToSF() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        prefs.setString('stringValue', "abc");
      }

     getStringValuesSF() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        String stringValue = prefs.getString('stringValue');
        return stringValue;
      }
    }
rommy
  • 185
  • 2
  • 9
  • 1
    The error comes because `getStringValuesSF` returns a future. Either use the pref declared earlier and change the function to a normal one instead of async or use futurebuilder – Midhun MP Apr 28 '20 at 20:04
  • Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – Christopher Moore Oct 02 '20 at 17:36

2 Answers2

1

default async function return dynamic we have to do type casting

Future<String> getStringValuesSF() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        String stringValue = prefs.getString('stringValue');
        return stringValue;
      }
Abhishek Ghaskata
  • 1,802
  • 2
  • 6
  • 11
0

I will just extend answer from @Abhishek as I needed similar but didn't work as epxected on TextFormField.

So I made up a bare loadString method to get any kind of key from sharedPrefs:

  Future<String> loadString(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(key) ?? '';
  }

Next in the same class I created init form void to use above method (I still think this way of working with Future is bit not optimal in Dart, anyway..), this will load data into controller:

Future<void> _initForm() async {
    final clientBusinessRegistrationID = await loadString('clientBusinessRegistrationID');
    _clientBusinessRegistrationIDController.value =
        _clientBusinessRegistrationIDController.value.copyWith(
            text: clientBusinessRegistrationID);
}

I also added this block in same class:

 SharedPreferences? preferences;
  Future<void> initializePreference() async{
    preferences = await SharedPreferences.getInstance();
  }

and finally in initState() I call it and it works:

  @override
  void initState()   {
    super.initState();
    // setupLocator();

    initializePreference().whenComplete((){
      setState(() {});
    });

    _clientBusinessRegistrationIDController.text = 'Initial';
    _initForm();
}
kensai
  • 943
  • 9
  • 16