1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] AppLifeCycleState.detached is not called When App is closed

Discussão em 'Mobile' iniciado por Stack, Outubro 1, 2024 às 11:04.

  1. Stack

    Stack Membro Participativo

    Future<void> onAppClose({required String userId}) async {
    await FirebaseFirestore.instance
    .collection(FirebaseConstants.usersCollection)
    .doc(userId)
    .update({'isOnline': false});
    }

    @override
    void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    }

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

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
    final userId = ref.read(currentDeliveryUserProvider)?.reference.id;
    print("state--------------------$state");
    if (userId != null) {
    if (state == AppLifecycleState.detached) {
    // The app is completely closing
    onAppClose(userId: userId);
    }
    }
    }

    @override
    Widget build(BuildContext context) {
    h = MediaQuery.of(context).size.height;
    w = MediaQuery.of(context).size.width;
    return GestureDetector(
    onTap: () {
    FocusManager.instance.primaryFocus?.unfocus();
    },
    child: MaterialApp(
    title: 'ONtrend Partners',
    home: const SplashScreen(),
    // home: TestFile(),
    theme: ThemeData(
    textTheme: GoogleFonts.dmSansTextTheme(),
    ),
    debugShowCheckedModeBanner: false,
    ),
    );
    }
    }


    I'm building a delivery app in Flutter, and I'm trying to set the user's isOnline status to false in Firebase when the app is closed. I've been using WidgetsBindingObserver to listen for AppLifecycleState changes, and everything works fine for AppLifeCycleState.paused and AppLifeCycleState.inactive. However, I noticed that AppLifeCycleState.detached is not being triggered when the app is completely closed (terminated).

    Continue reading...

Compartilhe esta Página