onDestroy or dispose in flutter

2.7k views Asked by At

I am developing a note app, and I want to save my note when I closed the app (when removing the app from recent apps). I tried dispose() method but it did not work. I tried :

@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
    final isDetached = state == AppLifecycleState.detached;}

but it did not work too. it does not print detached status, just it prints AppLifecycleState.paused AppLifecycleState.inactive AppLifecycleState.resume

so what should I do here!!

4

There are 4 answers

1
Yash Bhansali On

You can always use shared preferences in that case. Share preferences are used to save data in your local storage and the implementation is also very simple. and don't worry it won't make your app laggy or slow. It works very smoothly

https://pub.dev/packages/shared_preferences

do upvote if helpful

2
Er1 On

Your code should look something like this if you're observing the app lifecycle state:

class AppLifecycleReactor extends StatefulWidget {
  const AppLifecycleReactor({ Key? key }) : super(key: key);

  @override
  State<AppLifecycleReactor> createState() => _AppLifecycleReactorState();
}

class _AppLifecycleReactorState extends State<AppLifecycleReactor> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    print(state);
  }

  @override
  Widget build(BuildContext context) {
    return Widgets;
  }
}

to summarise:

  • with WidgetsBindingObserver after extends State
  • WidgetsBinding.instance!.addObserver(this); in initstate
  • WidgetsBinding.instance!.removeObserver(this); in dispose

For more info check: https://api.flutter.dev/flutter/widgets/WidgetsBindingObserver-class.html

2
Littlepea On

dispose() function is only visible in StatefulWidget

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final AppRouter _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return BlocProvider<CounterCubit>(
      create: (context) => CounterCubit(),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        onGenerateRoute: _appRouter.onGenerateRoute,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    // dispose your code here
  }
}
0
Georg On

Please consider using Firebase and save for example after every edit, or after 500ms after an edit using rx.debounce from rxdart.

You cannot rely on the app really being able to execute dispose or even save something to a database when it is closed. Removing an app from the recent list ends the app very quickly (kills the app), that's why it works even for hung/crashed apps.