Home > other >  Flutter SharedPreferences.getInt('key') returning "Instance of Future<dynamic>&
Flutter SharedPreferences.getInt('key') returning "Instance of Future<dynamic>&

Time:06-03

I'm trying to assign the result of an getInt('key') to an int variable with a function, but it returns "Instance of Future". I don't know how to get the value out of this... Here is the following code:

getMainColorInt() async {
  final prefs = await SharedPreferences.getInstance();
    
  final int? mainColorInt = prefs.getInt("mainColorInt");
    
  return prefs.getInt("mainColorInt");
}

class Palette {
static int colorInt = getMainColorInt();
}

When I try to print(getMainColorInt()) it returns "Instance of Future< dynamic >"

CodePudding user response:

static int colorInt = await getMainColorInt();

because the funtion is async:

getMainColorInt() async {

CodePudding user response:

I founded a solution; you just have to do this :

Palette.colorInt = prefs.getInt("mainColorInt");

  • Related