when my internet is cut and reconnect, the subscription is changed and i do not get the updates how can i reconnect to the subscription or how can i close it
You could add an EventListener on window for offline to detect when you lose connection. You can then call the unsubscribe() function to close the connection.
Then you can listen for online again, and on reconnect, either reload the page, or recreate the subscribe.
I think he using flutter for this issue because base on the tagged its flutter indication
You can still adapt the procedure of @ideclon in flutter but that depends on what state management your app used. In instance, if riverpod you can actually use the AutoDisposeAsyncNotifier here. and call the ref.invalidate to refresh the provider
Whoops, sorry! Should have checked the tags!
final consultationRolesControllerProvider =
AutoDisposeAsyncNotifierProvider<ConsultationRolesController, List<Role>>(
() => throw UnimplementedError());
class ConsultationRolesController extends AutoDisposeAsyncNotifier<List<Role>> {
@override
FutureOr<List<Role>> build() async {
state = const AsyncValue.loading();
ref.onDispose(() {
_subscription?.cancel();
});
final consultId = ref.read(consultationdIdProvider);
final roles = await ref.read(consultationRepoProvider).getRoles(queries: [
Query.equal("consultationRef", consultId),
]);
roles.removeWhere((element) => element.name == "owner");
subscribe();
return roles;
}
StreamSubscription<ConsultationRealtime<ConsultationRole>>? _subscription;
void subscribe() {
_subscription = ref
.read(consultationRepoProvider)
.getConsultationRoleStream(
Realtime(AppwriteClient.instance.client),
)
.listen((roles) => _rolesListener(roles),
onError: _rolesErrorListener, cancelOnError: true);
}
void _rolesErrorListener(Object error, StackTrace stackTrace) {
state = AsyncValue.error(error, stackTrace);
}
void _rolesListener(ConsultationRealtime<ConsultationRole> roles) {
if (roles.type == ConsultationRealtimeType.create) {
final merge = [...state.value!, roles.data];
state = AsyncData(merge);
} else if (roles.type == ConsultationRealtimeType.delete) {
final merge = [...state.value!];
merge.removeWhere((element) => element.id == roles.data.id);
state = AsyncData(merge);
} else if (roles.type == ConsultationRealtimeType.update) {
final merge = [...state.value!];
final index = merge.indexWhere((element) => element.id == roles.data.id);
merge[index] = roles.data;
state = AsyncData(merge);
}
}
As you can see in build method we have
ref.onDispose(() {
_subscription?.cancel();
});
Now if you trigger this ref.invalidate(consultationRolesControllerProvider) it will refresh your provider and that ref.onDispose will also be triggered. And that Happen your provider will be refreshed
Recommended threads
- Relationship lists aren't showing
In flutter, when I perform a listRows function for my table which contains various relationships in addition to normal data, I am not getting the relationships ...
- Error with realtime channels
I'm performing a subscription to realtime channels, and after a few seconds I get an exception with this error: {\"type\":\"error\",\"data\":{\"code\":1008,\"me...
- Which flutter SDK version for Self Hoste...
Hi all, Is there a good way to figure out which version of flutter SDK and Dart SDK is current for latest available self-hosted 1.8.0 ? I know new features are...