I'm tring to set/get sharedpreferences with no luck , my code throw exception in setState(Writing) and InitState (Reading) .
My code :
class _MyHomePageState extends State<MyHomePage> {
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
int _counter = 0;
Future<void> _incrementCounter() async {
final SharedPreferences prefs = await _prefs;
final int counter = (prefs.getInt('counter') ?? 0) 1;
setState(() {
_counter = prefs.setInt('counter', counter).then((bool success) { //error here
return counter;
});
});
}
@override
void initState() {
super.initState();
_counter = _prefs.then((SharedPreferences prefs) { // error here
return prefs.getInt('counter') ?? 0;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
_incrementCounter();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
exception tells me that can't cast Future to int
Error: A value of type 'Future' can't be assigned to a variable of type 'int'
CodePudding user response:
I think if you say await prefs.setint().then(... It should work, but it is also confusing to mix the .then syntax with the await syntax for futures.
So it might be more clear to say: final result = await prefs.setInt(); if(result) { setState (...
CodePudding user response:
The flutter compiler is right to point out that you are trying to assign a Future to an int value.
A Future in Dart is basically a value, which will be available later (in the future), hence directly assigning the value to it does not make sense. Try waiting for the future using await
or register a callback using .then()
.
Your snippet modified the below-mentioned way should work.
class _MyHomePageState extends State<MyHomePage> {
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
int _counter = 0;
Future<void> _incrementCounter() async {
final SharedPreferences prefs = await _prefs;
final int counter = (prefs.getInt('counter') ?? 0) 1;
prefs.setInt('counter', counter).then((bool success) {
setState(() {
_counter = counter;
});
});
}
@override
void initState() {
super.initState();
_prefs.then((SharedPreferences prefs) => prefs.getInt('counter') ?? 0).then((value) => _counter = value);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
_incrementCounter();
},
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
Read more about Futures
here. The Dart documentation is extremely detailed about this.