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.
Ok, so i tried to comment my issues the best I can. Hope you see what I'm trying to accomplish.
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);
}
});```
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;
},
);
}```
backgroundColor: AppColors.background,
body: locations.when(
data: (data) { ... Listview builder here using data returned
One thing that might be a problem is your realtime instance is being recreated with each build
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
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());
};
});```
Never mind it still getting errors
Why do you need the subscription active value notifier?
How many times does locations emit data?
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();
}
};
});```
It only retrieves data once. The websocket closes after a while so i need that value to resubscribe.
On the done() method of the stream.
Why use a value notifier?
well i can probably do useState but really does it make a difference?
maybe it does i'll try to change it
Oh, your talking about the subscription method.
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
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.
Flutter Hooks with Realtime [SOLVED]
[SOLVED] Flutter Hooks with Realtime
Recommended threads
- Current User is Not authorized
recreating same Thread
- Apple OAuth Scopes
Hi Hi, I've configured sign in with apple and this is the response i'm getting from apple once i've signed in. I cant find anywhere I set scopes. I remember se...
- Sign In With Apple OAuth Help
Hi All! I've got a flutter & appwrite app which Im trying to use sign in with apple for. I already have sign in with google working and the function is the sam...