我想检测用户何时退出我的应用程序并执行一些代码,但我不知道如何执行此操作。我尝试使用这个包:https://pub.dev/packages/flutter_lifecycle_state https://pub.dev/packages/flutter_lifecycle_state但我有这个错误:
flutter/.pub-cache/hosted/pub.dartlang.org/flutter_lifecycle_state-1.0.0/lib/flutter_lifecycle_state.dart:80:30:错误:找不到吸气剂:“暂停”。
案例 AppLifecycleState.suspending
如果您对这个问题有任何解决方案,或者知道另一种方法来检测用户何时退出我的应用程序,那可能会很酷
您现在无法完全执行您想要执行的操作,无论如何,现在最好的方法是使用 SDK 中的 AppLifecycleState 检查应用程序何时在后台运行/处于非活动状态(基本上执行您的库正在尝试执行的操作)
您正在使用的库已过时,因为 2019 年 11 月的拉取请求AppLifecycleState.suspending
它被称为AppLifecycleState.detached
.
您可以查看 AppLifecycleState 枚举api.flutter.dev https://api.flutter.dev/flutter/dart-ui/AppLifecycleState.html website
以下是如何观察包含活动的生命周期状态的示例:
import 'package:flutter/widgets.dart';
class LifecycleWatcher extends StatefulWidget {
@override
_LifecycleWatcherState createState() => _LifecycleWatcherState();
}
class _LifecycleWatcherState extends State<LifecycleWatcher> with WidgetsBindingObserver {
AppLifecycleState _lastLifecycleState;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
setState(() {
_lastLifecycleState = state;
});
}
@override
Widget build(BuildContext context) {
if (_lastLifecycleState == null)
return Text('This widget has not observed any lifecycle changes.', textDirection: TextDirection.ltr);
return Text('The most recent lifecycle state this widget observed was: $_lastLifecycleState.',
textDirection: TextDirection.ltr);
}
}
void main() {
runApp(Center(child: LifecycleWatcher()));
}
我认为在非活动周期中删除数据,然后在恢复周期中再次创建数据可以为您提供帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)