There are 2 functions. One must return String other save this String in with SharedPreferences.
The problem is, that by using prefs.getString()
I get not a String but another object.
The error called: A value of type 'String?' can't be assigned to a variable of type 'String'.
getCurrentCityFromSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString('currentCity');
}
Future<void> setCurrentCityInSharedPreferences(String newCity) async{
final prefs = await SharedPreferences.getInstance();
prefs.setString('currentCity', newCity);
}
I have tried to rewrite function to
Future<String?> getCurrentCityFromSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString('currentCity');
}
but then I get as string Deskription of object: Instance of 'Future<String?>'
your set string type is simply string and get type is String? so change set type like this