Search code examples
flutterdartsharedpreferencesvariable-assignmentreturn-type

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


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 >"


Solution

  • I found a solution, you just have to do this:

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