diff --git a/lib/providers/providers.dart b/lib/providers/providers.dart index b4b0947..d766883 100644 --- a/lib/providers/providers.dart +++ b/lib/providers/providers.dart @@ -3,6 +3,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../api/types.dart'; import '../api/client.dart'; +import '../api/grpc_client.dart'; +import '../api/grpc_config.dart'; import '../api/openapi_config.dart'; import '../services/auth_service.dart'; import '../models/user_profile.dart'; @@ -21,6 +23,31 @@ final apiClientProvider = Provider((ref) { ); }); +/// Provider for the gRPC-based CQRS API client. +/// +/// Uses auto-dispose to properly shut down the gRPC channel when the provider +/// is no longer being watched. This ensures network resources are cleaned up. +/// +/// Example usage: +/// ```dart +/// final grpcClient = ref.watch(grpcClientProvider); +/// final result = await grpcClient.getDeliveryRoutes(); +/// ``` +final grpcClientProvider = Provider.autoDispose((ref) { + final authService = ref.watch(authServiceProvider); + final client = GrpcCqrsApiClient( + config: GrpcConfig.development, + authService: authService, + ); + + // Register disposal callback to clean up gRPC channel resources + ref.onDispose(() { + client.shutdown(); + }); + + return client; +}); + final isAuthenticatedProvider = FutureProvider((ref) async { final authService = ref.watch(authServiceProvider); return await authService.isAuthenticated();