I want to use Shared Preferences in the model and save the data, but it doesn't work.
For example, in the example below, I would like to save the counter of the default count-up app in the model so that the previous counter appears on the screen when I close the app and start it again, but when I restart it, it says zero.Press the plus button to display the previous number plus 1.
In this case, please let me know what the problem is.
import'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(
ChangeNotifierProvider<MyHomePageModel>(
create:(_)=>MyHomePageModel(),
child —MyApp(),
)
);
}
class MyApp extensions StatelessWidget {
// This widget is the root of your application.
@override
Widget build (BuildContext context) {
US>return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwitch:Colors.blue,
visualDensity —VisualDensity.adaptivePlatformDensity,
),
home —MyHomePage(),
);
}
}
class MyHomePage extensions StatelessWidget{
@override
Widget build (BuildContext context) {
return Consumer<MyHomePageModel>(builder:(context,model,child){
int_counter=model.getCounter();
US>return Scaffold(
appBar —AppBar(
title:Text('FlutteR Demo Home Page'),
),
body: Center(
child —Column(
mainAxisAlignment—MainAxisAlignment.center,
children:<Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style:Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton—Button());
});
}
}
class Button extensions StatelessWidget {
@override
Widget build (BuildContext context) {
final model=Provider.of<MyHomePageModel>(context,listen:false);
return FloatingActionButton(
onPressed: ( ) = > model.addCounter(),
tooltip: 'Increment',
child —Icon(Icons.add),
);
}
}
class MyHomePageModel extensions ChangeNotifier {
int_counter = 0;
void_setPrefItems()async {
SharedPreferences prefs = wait SharedPreferences.getInstance();
prefs.setInt('counter',_counter);
}
void_getPrefItems()async {
SharedPreferences prefs = wait SharedPreferences.getInstance();
_counter=prefs.getInt('counter')??0;
}
void addCounter() {
_counter++;
_setPrefItems();
notifyListeners();
}
intgetCounter(){
_getPrefItems();
return_counter;
}
}
Explain why the behavior is as described in the questionnaire.
0 appears on reboot.
Consider the first build of MyHomePage.where model.getCounter();
returns 0.Why is that?
_getPrefItems
is called in getCounter
of MyHomePageModel, but _getPrefItems
is an asynchronous function. _counter=prefs.getInt('counter') ??0;
is called return> before
.At this point
_counter
is the initial value of 0.The MyHomePageModel _counter
is then set to prefs.getInt('counter')
, but MyHomePage is not redrawn because there is no notifyListeners
.
Press the plus button to display the previous number plus 1.
Press the plus button to call the MyHomePageModel addCounter._counter++
runs, and then notifyListeners
is called. When notifyListeners
is called, MyHomePage is redrawn, and getCounter
returns "previous number plus 1".
An easy solution would be to add notifyListeners
to _getPrefItems
.
void_getPrefItems()async{
SharedPreferences prefs = wait SharedPreferences.getInstance();
_counter=prefs.getInt('counter')??0;
// addition
notifyListeners();
}
© 2024 OneMinuteCode. All rights reserved.