停止应用程序后我无法保存状态或数据,这就是问题所在。即使关闭应用程序后,我也希望它保留我以前的数据或状态。我可以通过什么方式来实现这个目标?
对于状态管理,我使用 Flutter Bloc。为了您更好地理解,下面是我的虚拟代码。在这里,即使在关闭应用程序后,我也想保留当前状态。但是,我无法恢复关闭程序之前正在浏览的信息或当前状态。
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BlocProvider(
create: (context) => LocationBloc(),
child: HomeScreen(),
),
);
}
}
class LocationBloc extends Bloc<LocationEvent, LocationState> {
LocationBloc() : super(null);
@override
Stream<LocationState> mapEventToState(LocationEvent event) async* {
switch (event) {
case LocationEvent.selectGPS:
yield GPSState();
break;
case LocationEvent.selectLocation:
yield LocationState();
break;
}
}
}
class GPSState extends LocationState {}
class LocationState extends LocationState {}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final LocationBloc _locationBloc = BlocProvider.of<LocationBloc>(context);
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: BlocBuilder<LocationBloc, LocationState>(
builder: (context, state) {
return Center(
child: Text(
state.runtimeType == GPSState ? 'GPS' : 'Location',
style: TextStyle(fontSize: 24),
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SettingsScreen()),
);
},
child: Icon(Icons.settings),
),
);
}
}
class SettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
final LocationBloc _locationBloc = BlocProvider.of<LocationBloc>(context);
return Scaffold(
appBar: AppBar(
title: Text('Settings'),
),
body: BlocBuilder<LocationBloc, LocationState>(
builder: (context, state) {
return Column(
children: [
RadioListTile(
title: Text('GPS'),
value: GPSState(),
groupValue: state,
onChanged: (value) {
_locationBloc.add(LocationEvent.selectGPS);
},
),
RadioListTile(
title: Text('Location'),
value: LocationState(),
groupValue: state,
onChanged: (value) {
_locationBloc.add(LocationEvent.selectLocation);
},
),
],
);
},
),
);
}
}
这就是我应对这一挑战的方式。
1 获取应用生命周期。
class _AppLifecycleReactorState extends State with WidgetsBindingObserver {
@override
void initState(){
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
@override
void dispose(){
super.dispose();
WidgetsBinding.instance!.removeObserver(this);
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
print("App Lifecycle State : $state");
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
2 - 您可以使用 shared_preferences 包在应用程序启动时保留数据。
这是一个例子:
import 'package:shared_preferences/shared_preferences.dart';
void storeData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('key', 'value');
}
然后,当您的应用程序再次启动时,您可以像这样加载存储的数据:
void loadData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String value = prefs.getString('key');
}
3 - 当 AppLifecycle 发生变化时写入数据,并在打开应用程序时从首选项中读取数据。
注意:
在某些情况下(例如当操作系统强制关闭您的应用程序时),您可能没有足够的时间将数据可靠地保存到磁盘。