Back

[SOLVED] Realtime in Flutter

  • 0
  • Flutter
  • Realtime
djcali
10 Apr, 2023, 20:28

Hello, This is more of a best practice question, in Flutter I retrieve some documents into a list when the user opens the app. When the user clicks on a document it goes to a details page. I only want to listen to realtime events for that specific document, while user is viewing. Where is the best place to subscribe to the realtime events of this document? Should it be in the initState or soon as the user clicks the item in the list? also I'm assuming on this page I should close the subscription in the dispose() method. Thanks for any suggestions.

TL;DR
The user is looking for advice on where to subscribe to realtime events for a specific document in a Flutter app. They are currently subscribing in the `initState` method and closing the subscription in the `dispose` method. They are experiencing an error when trying to resubscribe after the widget is deactivated. The recommended solution is to use a private variable as a flag in the `dispose` method and update it to prevent resubscribing when the widget is deactivated. Another user suggests creating a new Realtime instance in the details page to avoid any race condition problems.
Binyamin
10 Apr, 2023, 20:33

I think the best way would be init in initState as this is the best way to interact with the state And close it in the dispose method This way you can assure you will listened to realtime event only and when it needed

Drake
10 Apr, 2023, 21:28

ya, this probably makes sense. @djcali, make sure you're using a new Realtime instance in this details page to make sure there's no race condition problem

djcali
10 Apr, 2023, 22:10

Oh man, I was wondering why I was having an issue with a bunch of realtime connections. It was doubling up. Thanks!

djcali
10 Apr, 2023, 22:51

Works perfectly. Thanks for all the help.

djcali
11 Apr, 2023, 00:10

Hi, I have one issue. I'm not sure the correct way to resubscribe when it gets disposed by Flutter randomly. Here is what I have so far. I'm getting error trying to access a deactivated widget, because the way i'm resubscribing.

TypeScript
    super.initState();
    subscribe();
  }

late RealtimeSubscription subscription;

  void subscribe() {
    debugPrint('Subscribing to realtime');

    final client = ref.read(appwriteClientProvider);

    final realtime = Realtime(client);

    subscription = realtime.subscribe([
      'databases.default.collections.events.documents.642ba977cff76a18a36c'
    ]);

    subscription.stream.listen((response) {
      print(response);
    }, onDone: () {
      subscribe();
    });
  }

  @override
  void dispose() {
    subscription.close();
    super.dispose();
  } ```
Binyamin
11 Apr, 2023, 00:12

What is the error you're getting

djcali
11 Apr, 2023, 00:16

When I leave the Details page the Dispose() method fires then the onDone of the listen function fires because I'm trying to resubscribe. I want this to work unless I'm leaving the page. Hope this makes sense.

Binyamin
11 Apr, 2023, 00:17

Got you What I usually do in this use-case is creating a flag that I will update in the dispose method like this.

TypeScript
private _isDone = false;

void initState() {
    super.initState();
    subscribe();
  }

late RealtimeSubscription subscription;

  void subscribe() {
    debugPrint('Subscribing to realtime');

    final client = ref.read(appwriteClientProvider);

    final realtime = Realtime(client);

    subscription = realtime.subscribe([
      'databases.default.collections.events.documents.642ba977cff76a18a36c'
    ]);

    subscription.stream.listen((response) {
      print(response);
    }, onDone: () {
      if(!_isDone)
        subscribe();
    });
  }

  @override
  void dispose() {
    _isDone = true;
    subscription.close();
    super.dispose();
  } 
djcali
11 Apr, 2023, 00:20

I think I tried something like this but I wasn't using a private variable. Going to try your way and report back. Thanks!

djcali
11 Apr, 2023, 00:25

Worked perfectly. Thanks!

Binyamin
11 Apr, 2023, 00:26

Perfect 😄

djcali
11 Apr, 2023, 18:41

[SOLVED] Realtime in Flutter

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