Issue
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'
Solution
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.
Answered By - Swanav
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.