Index
Index
In Flutter Riverpod 2, to get the current latitude and longitude, you typically
integrate with the geolocator or location package. Since these involve
asynchronous and possibly one-time or streaming values, the right type of
Riverpod provider depends on how you want to access location data:
✅ Common Options
1. For one-time fetch (e.g., current location once):
Use a FutureProvider.
dart
Copy
Edit
final currentLocationProvider = FutureProvider((ref) async {
// Ensure permissions are handled before this
return await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
});
2. For continuous updates (e.g., tracking location):
Use a StreamProvider.
dart
Copy
Edit
final locationStreamProvider = StreamProvider((ref) {
return Geolocator.getPositionStream(
locationSettings: const LocationSettings(
accuracy: LocationAccuracy.high,
1 / 11
2025-05-07
distanceFilter: 10,
),
);
});
💡 Don't forget:
You need to request permission before accessing the location:
dart
Copy
Edit
await Geolocator.requestPermission();
Or check and handle permission status properly.
👇 Example Widget
class LocationWidget extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final locationAsync =
ref.watch(currentLocationProvider);
return locationAsync.when(
data: (position) => Text(
'Lat: ${position.latitude}, Lon:
${position.longitude}'),
loading: () => CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'),
);
}
}
2 / 11
2025-05-07
Simple example
✅ Step-by-step:
1. Add dependencies in pubspec.yaml
yaml
Copy
Edit
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^2.4.0
geolocator: ^10.1.0
2. Main Dart File (main.dart)
dart
Copy
Edit
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:geolocator/geolocator.dart';
3 / 11
2025-05-07
void main() {
runApp(const ProviderScope(child: MyApp()));
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Location with Riverpod',
theme: ThemeData(primarySwatch: Colors.blue),
home: const LocationPage(),
);
}
}
3. Providers (location_provider.dart)
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:geolocator/geolocator.dart';
4 / 11
2025-05-07
});
4. UI Page (location_page.dart)
dart
Copy
Edit
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'location_provider.dart';
@override
Widget build(BuildContext context, WidgetRef ref) {
final locationAsync = ref.watch(currentLocationProvider);
5 / 11
2025-05-07
return Scaffold(
appBar: AppBar(title: const Text('Your Location')),
body: Center(
child: locationAsync.when(
data: (position) => Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Latitude: ${position.latitude}'),
Text('Longitude: ${position.longitude}'),
],
),
loading: () => const CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'),
),
),
);
}
}
🔐 Android Permissions (AndroidManifest.xml)
Add inside the tag:
xml
Copy
Edit
6 / 11
2025-05-07
✅ Summary:
FutureProvider handles the async location request.
Let me know if you want this to also support live updates using StreamProvider,
or want to use location package instead!
You said:
in location_page.dart want to redirect another route when read lat, long using go
router
2/2
ChatGPT said:
Perfect! You can redirect to another route using GoRouter after the latitude and
longitude are successfully fetched.
7 / 11
2025-05-07
import 'package:go_router/go_router.dart';
import 'location_provider.dart';
@override
Widget build(BuildContext context, WidgetRef ref) {
final locationAsync = ref.watch(currentLocationProvider);
return Scaffold(
appBar: AppBar(title: const Text('Getting Location...')),
body: Center(
child: locationAsync.when(
data: (_) => const CircularProgressIndicator(), //
Redirect will happen soon
loading: () => const CircularProgressIndicator(),
error: (err, stack) => Text('Error: $err'),
),
8 / 11
2025-05-07
),
);
}
}
🗺 Example GoRouter Setup (main.dart or router.dart)
dart
Copy
Edit
final _router = GoRouter(
routes: [
GoRoute(
path: '/',
builder: (context, state) => const LocationPage(),
),
GoRoute(
path: '/result',
builder: (context, state) {
final lat = state.uri.queryParameters['lat'];
final lng = state.uri.queryParameters['lng'];
return ResultPage(latitude: lat!, longitude: lng!);
},
),
],
);
🧭 Example Result Page (result_page.dart)
dart
9 / 11
2025-05-07
Copy
Edit
import 'package:flutter/material.dart';
const ResultPage({
super.key,
required this.latitude,
required this.longitude,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Location Result')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Latitude: $latitude'),
Text('Longitude: $longitude'),
],
),
),
);
}
10 / 11
2025-05-07
}
Let me know if you want to pass the values using extra or state object instead of
query parameters!
11 / 11