'onDestroy or dispose in flutter

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!!



Solution 1:[1]

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

Solution 2:[2]

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
  }
}

Solution 3:[3]

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.

Solution 4:[4]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Er1
Solution 2 Littlepea
Solution 3 Georg
Solution 4 Yash Bhansali