所以我想知道为什么提供者在浏览器刷新时会丢失状态。刷新/重新加载后不应该保持状态吗?
真的很感激帮助
颤振的默认项目
home: ChangeNotifierProvider<Counter>(
create: (_) => Counter(),
child: MyHomePage(title: 'Flutter Demo Home Page')),
class MyHomePage extends StatelessWidget {
final String title;
MyHomePage({this.title});
@override
Widget build(BuildContext context) {
final counter = Provider.of<Counter>(context);
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many
times:',
),
Text(
'${counter.value}',
style:
Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => counter.increment(),
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
} …Run Code Online (Sandbox Code Playgroud)