0% found this document useful (0 votes)
7 views6 pages

FutureProvider - Family, and Shows How You Can Fetch and Display A Voucher Properly in Flutter.

The document provides an updated code example using Flutter Riverpod to manage location permissions and continuous location updates with Geolocator. It includes providers for checking location permissions, streaming location updates, and extracting latitude and longitude from the stream. Additionally, it explains how to use these providers in widgets to display location data and offers guidance on combining one-time fetches with live updates.

Uploaded by

Ko Aung Thu Oo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

FutureProvider - Family, and Shows How You Can Fetch and Display A Voucher Properly in Flutter.

The document provides an updated code example using Flutter Riverpod to manage location permissions and continuous location updates with Geolocator. It includes providers for checking location permissions, streaming location updates, and extracting latitude and longitude from the stream. Additionally, it explains how to use these providers in widgets to display location data and offers guidance on combining one-time fetches with live updates.

Uploaded by

Ko Aung Thu Oo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2025-05-07

Updated Code Using StreamProvider:

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:geolocator/geolocator.dart';

/// Check and request location permission


final locationPermissionProvider = FutureProvider<bool>
((ref) async {
LocationPermission permission = await
Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
return permission == LocationPermission.always ||
permission == LocationPermission.whileInUse;
});

/// StreamProvider to listen to continuous location updates


final locationStreamProvider = StreamProvider<Position>
((ref) async* {
final hasPermission = await
ref.watch(locationPermissionProvider.future);
if (!hasPermission) {
throw Exception('Location permission denied');
}

// Yield the stream of position updates


yield* Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,

1/6
2025-05-07

distanceFilter: 10, // Minimum movement (in meters)


before update
),
);
});

🧠 How to Use It in a Widget:


final location = ref.watch(locationStreamProvider);

location.when(
data: (position) => Text('Lat: ${position.latitude}, Lng:
${position.longitude}'),
loading: () => CircularProgressIndicator(),
error: (e, _) => Text('Error: $e'),
);

📝 Notes:
distanceFilter helps reduce noise—location updates only fire if the user moves 10
meters (you can adjust this).

If you want one-time fetch only, keep your currentLocationProvider.

If you want live updates, use locationStreamProvider.

Want to combine both—e.g., fetch once initially and then listen for changes? I can
show you that too!

2/6
2025-05-07

1. Accessing Location in Another Provider:


Let's assume you want to create a provider that extracts the latitude and
longitude from the stream and provides them separately.

final latitudeProvider = Provider<double>((ref) {


final locationAsyncValue =
ref.watch(locationStreamProvider);

return locationAsyncValue.when(
data: (position) => position.latitude,
loading: () => 0.0, // Or handle it however you like
error: (e, _) => 0.0, // Handle the error
);
});

final longitudeProvider = Provider<double>((ref) {


final locationAsyncValue =
ref.watch(locationStreamProvider);

return locationAsyncValue.when(
data: (position) => position.longitude,
loading: () => 0.0, // Or handle it however you like
error: (e, _) => 0.0, // Handle the error
);
});

2. Using the Latitude and Longitude in Your Widget:


Now, you can use these providers in your widgets to get the latitude and
longitude.

3/6
2025-05-07

final latitude = ref.watch(latitudeProvider);


final longitude = ref.watch(longitudeProvider);

Text('Lat: $latitude, Lng: $longitude');

3. Complete Example:

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:geolocator/geolocator.dart';

/// Check and request location permission


final locationPermissionProvider = FutureProvider<bool>
((ref) async {
LocationPermission permission = await
Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
return permission == LocationPermission.always ||
permission == LocationPermission.whileInUse;
});

/// StreamProvider to listen to continuous location updates


final locationStreamProvider = StreamProvider<Position>
((ref) async* {
final hasPermission = await
ref.watch(locationPermissionProvider.future);
if (!hasPermission) {
throw Exception('Location permission denied');

4/6
2025-05-07

// Yield the stream of position updates


yield* Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 10, // Minimum movement (in meters)
before update
),
);
});

/// Provider to access the current latitude from the


location stream
final latitudeProvider = Provider<double>((ref) {
final locationAsyncValue =
ref.watch(locationStreamProvider);

return locationAsyncValue.when(
data: (position) => position.latitude,
loading: () => 0.0, // Or handle it however you like
error: (e, _) => 0.0, // Handle the error
);
});

/// Provider to access the current longitude from the


location stream
final longitudeProvider = Provider<double>((ref) {
final locationAsyncValue =
ref.watch(locationStreamProvider);

return locationAsyncValue.when(

5/6
2025-05-07

data: (position) => position.longitude,


loading: () => 0.0, // Or handle it however you like
error: (e, _) => 0.0, // Handle the error
);
});

Summary:
latitudeProvider and longitudeProvider: These providers extract latitude and
longitude from the stream provided by locationStreamProvider.

You can watch these providers anywhere in your app to get the current latitude
and longitude.

Let me know if you'd like more details or need help with any other part of the
app!

6/6

You might also like