How to pass the parameter getString need String? but read() used String
class SharedPref {
read(String key) async {
final prefs = await SharedPreferences.getInstance();
return json.decode(prefs.getString(key)); // the error in this line
}
this is for sharedPreferences of flutter. how to solve this?
4
Answers
The line
prefs.getString(key)
returns aString?
. It means that can return a String or a null value. You have to ensure that the value will not be null when passed tojson.decode()
.Check out the documentation for more info.
Try this:
Update your code
It’s cause of
getString
method have return typeString?
But
json.decode
takesString
as parameterSo you must pass
String
intojson.decode
method. There are many ways to do this