0

I successfuly saved a string to sharedpreferences. But, I dont understand how to getString and use this a parameter llike below code.

My getstring method;

Future<String> getString(String param) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(param) ?? 'lang';

I try to send a string with fetchApps method. This string should comes from sharedpreferences getString method.

return FutureBuilder<List<Apps>>(
        future: fetchApps(getString('lang')),
        builder: (c, s) {
...
jancooth
  • 555
  • 1
  • 10
  • 25

1 Answers1

1

your getString function is a Future You should use await in your getString('lang') like this:

return FutureBuilder<List<Apps>>(
        future: fetchApps(await getString('lang')),
        builder: (c, s) {
xion
  • 1,209
  • 11
  • 17
  • await getString('lang') and The argument type 'Future' can't be assigned to the parameter type 'String'. – jancooth Apr 16 '20 at 13:43
  • then, maybe u assign it to something else first? ```final bla = await getString('lang')``` before u passed it to your fetchApps ```fetchApps(bla)``` – xion Apr 16 '20 at 13:57
  • I did but still giving error. The await expression can only be used in an async function. Try marking the function body with either 'async' or 'async* – jancooth Apr 16 '20 at 14:02
  • yes, you need to make your function as async ```yourFunction async { final bla = await getString('lang') } ``` – xion Apr 16 '20 at 14:06
  • Ok, i got it. I applied your reply in my fetchApps method. It works. – jancooth Apr 16 '20 at 14:18
  • https://stackoverflow.com/questions/61170032/flutter-scrollcontroller-attached-to-multiple-scroll-views – jancooth Apr 16 '20 at 14:18