0

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 Answers4

0

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

Er1
  • 2,559
  • 1
  • 12
  • 24
  • Yes my code looks like this, but print method prints paused, inactive, resume, but when i close the app it will not print detach – wafaa sisalem Jan 18 '22 at 19:44
  • @wafaasisalem There is an issue on the GH regarding detached not being called https://github.com/flutter/flutter/issues/57594 – Er1 Jan 19 '22 at 09:15
0

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
  }
}
Littlepea
  • 155
  • 7
0

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.

Georg
  • 987
  • 8
  • 16
-1

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

Yash Bhansali
  • 420
  • 2
  • 6
  • Thank you, I know how to use shared preferences, but the thing is where should I use it? suppose you were writing your note and your phone closed or you closed the recent apps by mistake where should I put my code? – wafaa sisalem Jan 18 '22 at 15:42