Rank 3: Container
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.
Votes
0
Replies
24
Participants
Unknown
Messages
25
Rank 3: Container
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.
Rank 3: Container
Ok, so i tried to comment my issues the best I can. Hope you see what I'm trying to accomplish.
Rank 3: Container
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); } });```Rank 3: Container
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; }, ); }```Rank 3: Container
backgroundColor: AppColors.background, body: locations.when( data: (data) { ... Listview builder here using data returnedOne thing that might be a problem is your realtime instance is being recreated with each build
Rank 3: Container
Is there a way to check if there is an active subscription?
Not really...
If I subscribe, there's an active subscription so..
Or maybe you can look at the stream
Rank 3: Container
This is the only thing that works but it seems "hacky".
/// 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()); }; });```Rank 3: Container
Never mind it still getting errors
Why do you need the subscription active value notifier?
How many times does locations emit data?
Rank 3: Container
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.
/// 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(); } }; });```Rank 3: Container
It only retrieves data once. The websocket closes after a while so i need that value to resubscribe.
Rank 3: Container
On the done() method of the stream.
Why use a value notifier?
Rank 3: Container
well i can probably do useState but really does it make a difference?
Rank 3: Container
maybe it does i'll try to change it
Rank 3: Container
Oh, your talking about the subscription method.
Rank 3: Container
Well I need that to be able to update the state of the variable don't I?
You shouldn't put the subscription close in the when data callback
Rank 3: Container
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.
Rank 3: Container
Flutter Hooks with Realtime [SOLVED]
[SOLVED] Flutter Hooks with Realtime