Back

[SOLVED] Flutter Hooks with Realtime

  • 0
  • Flutter
djcali
16 Jul, 2023, 21:28

I can’t pass the subscription initially because it’s null I know this is an issue. When I get back to my computer I will paste better code.

TL;DR
The user is having trouble implementing Flutter Hooks with Realtime subscription. They are trying to eliminate extra code but are facing null check errors when closing the subscription. They have tried using `useEffect` and `useValueChanged` but are still encountering errors. The issue might be with the `realtime` instance being recreated with each build. The actual code and solution are not provided in the thread.
djcali
17 Jul, 2023, 01:35

Ok, so i tried to comment my issues the best I can. Hope you see what I'm trying to accomplish.

djcali
17 Jul, 2023, 01:38
TypeScript
    print('building');

    ref.watch(locationsProvider);

    /// Locations is a future, so when the data comes back the build method rebuilds.
    final locations = ref.watch(locationsListController); 

    /// Realtime variables
    RealtimeSubscription? subscription;
    final client = ref.read(appwriteClientProvider);
    final realtime = Realtime(client);
    final subscriptionActive = useState(false);

    useEffect(() {

      /// Subscribe to realtime only once here. The issue is when the locations provider
      /// finshes getting the data, this useEffect method reruns and throws a null check         /// error
      /// when disposing in the return method.

      subscriptionActive.value = true;
      subscribe(
          ref: ref, subscriptionActive: subscriptionActive, realtime: realtime);

      return () {
        /// When i leave the page this line tries to close the subscription
        /// but throws a null check error.
        subscription!.close();
      };
    });

    useValueChanged<bool, void>(subscriptionActive.value, (_, __) {
      if (subscriptionActive.value == false) {
        /// The done method executed from the subscribe method.
        /// resubscribe here
        subscriptionActive.value = true;
        subscribe(
            ref: ref,
            subscriptionActive: subscriptionActive,
            realtime: realtime);
      }
    });```
djcali
17 Jul, 2023, 01:38
TypeScript
    required WidgetRef ref,
    required ValueNotifier<bool> subscriptionActive,
    required Realtime realtime,
  }) {

    /// Subscribe to the locations documents
    RealtimeSubscription subscription = realtime.subscribe([
      'databases.default.collections.locations.documents',
    ]);

    /// Listen to realtime stream
    subscription.stream.listen(
      (response) {
        /// Get incoming event and filter out the collection and type of event
        List<String> parts = response.events[0].split('.');
        String collection = parts[3];
        String event = parts.last;

        print(parts);
        print(collection);
        print(event);
      },
      onDone: () {
        /// This line throws a ValueNotifer<bool> was used after being disposed.
        subscriptionActive.value = false;
      },
    );
  }```
djcali
17 Jul, 2023, 01:40
TypeScript
            backgroundColor: AppColors.background,
            body: locations.when(
                  data: (data) { ... Listview builder here using data returned
Drake
17 Jul, 2023, 01:47

One thing that might be a problem is your realtime instance is being recreated with each build

djcali
17 Jul, 2023, 02:08

Is there a way to check if there is an active subscription?

Drake
17 Jul, 2023, 02:11

Not really...

Drake
17 Jul, 2023, 02:11

If I subscribe, there's an active subscription so..

Drake
17 Jul, 2023, 02:13

Or maybe you can look at the stream

djcali
17 Jul, 2023, 02:14

This is the only thing that works but it seems "hacky".

TypeScript
      /// Subscribe to realtime only once here. The issue is when the locations provider
      /// finshes getting the data, this useEffect method reruns and throws a null check error
      /// when disposing in the return method.
      ///
      locations.whenData((value) {
        subscriptionActive.value = true;
        subscribe(
            ref: ref,
            subscriptionActive: subscriptionActive,
            realtime: realtime);
      });

      return () {
        /// When i leave the page this line tries to close the subscription
        /// but throws a null check error.
        ///

        locations.whenData((value) => subscription!.close());
      };
    });```
djcali
17 Jul, 2023, 02:18

Never mind it still getting errors

Drake
17 Jul, 2023, 02:21

Why do you need the subscription active value notifier?

How many times does locations emit data?

djcali
17 Jul, 2023, 02:23

This kind of works but I still get null check error when i leave the page. I just can't seem to close the subscription.

TypeScript
      /// Subscribe to realtime only once here. 
      if (!locations.isLoading) {
        subscriptionActive.value = true;
        subscribe(
            ref: ref,
            subscriptionActive: subscriptionActive,
            realtime: realtime);
      }

      return () {
        /// Getting a null check error here when i exit the page.
        if (!locations.isLoading) {
          subscription!.close();
        }
      };
    });```
djcali
17 Jul, 2023, 02:24

It only retrieves data once. The websocket closes after a while so i need that value to resubscribe.

djcali
17 Jul, 2023, 02:24

On the done() method of the stream.

Drake
17 Jul, 2023, 02:28

Why use a value notifier?

djcali
17 Jul, 2023, 02:29

well i can probably do useState but really does it make a difference?

djcali
17 Jul, 2023, 02:29

maybe it does i'll try to change it

djcali
17 Jul, 2023, 02:31

Oh, your talking about the subscription method.

djcali
17 Jul, 2023, 02:34

Well I need that to be able to update the state of the variable don't I?

Drake
17 Jul, 2023, 02:36

You shouldn't put the subscription close in the when data callback

djcali
17 Jul, 2023, 02:38

I give up. I'm just going to use a stateful widget and the dispose() method. lol. I know that works. I was just trying to eliminate extra code.

djcali
17 Jul, 2023, 20:21

Flutter Hooks with Realtime [SOLVED]

Drake
17 Jul, 2023, 20:32

[SOLVED] Flutter Hooks with Realtime

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more