Reputation: 33
I am developing an app with Flutter and Firebase.
I want to store the _id with SharedPreferences permanently.
Therefore, i looked after it, but my code doesnt work at all. It always throws the error:
type 'Future' is not a subtype of type 'String'
Here is my code:
class Profile with ChangeNotifier {
String _id;
void setName(String name) {
const url =
'myurl';
http
.post(url, body: json.encode({'name': name, 'description': name}))
.then((response) {
_id = json.decode(response.body)['name'];
});
addID();
}
Future<void> updateName(String name, String id) async {
String url =
'myurl';
await http.patch(url,
body: json.encode({'name': 'Ein Titel', 'description': name}));
}
And here are my methods with the SharedPrefs:
String getID() {
return getIDOffline();
}
addID() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('id', _id);
}
getIDOffline() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
String stringValue = prefs.getString('id');
return stringValue;
}
Upvotes: 1
Views: 100
Reputation: 323
You have use wrong method for return string so you have to change String getID()
to Future<String> getID()
. And you can use like this.
getValue()async{
String value = await getID();
}
Upvotes: 1
Reputation: 1182
When you use async always try to add also Future.
like :
Future<returnType> methodName() async { }
In your code try to change
String getID(){ }
to Future<String>getID() async{ }
Upvotes: 0