Riverpod with hooks beaks when widget is disposedI

948 Views Asked by At

I have Flutter mobile app that is using Riverpod with hooks.

I have the following function that I would like to be called when the widget is disposed:

    useEffect(
      () {
        final firestoreRepo = 
          ref.read(firebaseFirestoreRepositoryProvider);

        return () async {
          try {
            // I get exception at this line.  
            // I need this future to be called when the
            // widget is disposed.  
            // Calling this future earlier is not userful
            // for my business logic.
            final relationship =
              await ref.read(relationshipWithProvider(pid).future);

            if (relationship?.unseen ?? false) {
              await firestoreRepo?.updateRelatinoship(pid: pid);
            }
          } catch (e, st) {
            // print error
          }
        };
      },
      [],
    );

I keep getting this error at the line shown in the comment above.

I/flutter ( 5967): Looking up a deactivated widget's ancestor is unsafe.
I/flutter ( 5967): At this point the state of the widget's element tree is no longer stable.

How can I sold this problem

2

There are 2 best solutions below

2
Ruble On

We can initially get our relationship and then await and use it:

    useEffect(
      () {
        final firestoreRepo = ref.read(firebaseFirestoreRepositoryProvider);

        final relationship = ref.read(relationshipWithProvider(pid).future);

        return () async {
          try {
            if (await relationship?.unseen ?? false) {
              await firestoreRepo?.updateRelatinoship(pid: pid);
            }
          } catch (e, st) {
            // print error
          }
        };
      },
      [],
    );

As far as I can tell, this won't contradict the logic of the business process, because one way or another, we'll have to make the relationshipWithProvider(pid) request early (when we initialize the widget) or late (when we delete the widget).

0
Aimn Blbol On

I was able to solve the problem by moving the code into the WillPopScope widget like this. Code seems to be working so far.

     WillPopScope(
      onWillPop: () async {
        try {
          final relationship =
              await ref.read(relationshipWithProvider(pid).future);

          if (relationship?.unseen ?? false) {
            await ref
                .read(firebaseFirestoreRepositoryProvider)
                ?.updateRelatinoship(pid: pid);
          }
        } catch (e, st) {
          debugPrint('oolfa: ChatScreen - updateRelatinoship - error - $e');
          debugPrint('oolfa: ChatScreen - updateRelatinoship - stack- $st');
        }
        return true;
      },
      child: // rest of your code
``