Created
July 11, 2025 22:13
-
-
Save Collins-01/d8eec1ad8835ba6b75cc95cf486592df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | |
| import 'package:health/core/logger/logger.dart'; | |
| import 'package:health/data/model/location/location_model.dart'; | |
| import 'package:health/data/repository/location_repository.dart'; | |
| import 'package:health/di/setup.dart'; | |
| enum LocationStatusEnum { loading, loaded, error, initial } | |
| class LocationProviderState { | |
| final LocationStatusEnum status; | |
| final LocationModel? location; | |
| final String? error; | |
| LocationProviderState({required this.status, this.location, this.error}); | |
| } | |
| class LocationProvider extends StateNotifier<LocationProviderState> { | |
| final LocationRepository locationRepository = getIt<LocationRepository>(); | |
| LocationProvider() | |
| : super(LocationProviderState(status: LocationStatusEnum.initial)); | |
| Future<void> getUserLocation() async { | |
| try { | |
| state = LocationProviderState(status: LocationStatusEnum.loading); | |
| final result = await locationRepository.getUserLocation(); | |
| state = LocationProviderState( | |
| status: LocationStatusEnum.loaded, | |
| location: result, | |
| ); | |
| } catch (e) { | |
| AppLogger.error("Failed to fetch user location: $e"); | |
| state = LocationProviderState( | |
| status: LocationStatusEnum.error, | |
| error: e.toString(), | |
| ); | |
| } | |
| } | |
| } | |
| final locationProvider = StateNotifierProvider<LocationProvider, LocationProviderState>((ref) => LocationProvider()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment