Home > Net >  Flutter : How to use String outside async function?
Flutter : How to use String outside async function?

Time:08-15

GetUserData() is getting a String and turning it into String encodedMap and returning it but i cant seem to get turn this String global with static, Can anyone help me Make encodedMap a not future string that i can use everywhere

Future getUserData() async {
 SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
   
 String encodedMap = sharedPreferences.getString('TagMap') ?? '';
 return encodedMap;
}

CodePudding user response:

It is not possible since SharedPreferences.getInstance() returns a Future.

You have to await the call to getUserData().

CodePudding user response:

defined

late SharedPreferences sharedPreferences;

above of main function then in main function do this

void main() async {
WidgetsFlutterBinding.ensureInitialized();
sharedPreferences= await SharedPreferences.getInstance();;
runApp(const MyApp());
}

then when open app its be a global variable you can use it every where in app

  • Related