Challenge - 5 Problems
GPS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flutter widget showing GPS coordinates?
Consider this Flutter widget that tries to display the current latitude and longitude after fetching location. What will be shown on the screen after the location is fetched successfully?
Flutter
class LocationWidget extends StatefulWidget { @override State<LocationWidget> createState() => _LocationWidgetState(); } class _LocationWidgetState extends State<LocationWidget> { double? latitude; double? longitude; @override void initState() { super.initState(); fetchLocation(); } Future<void> fetchLocation() async { await Future.delayed(Duration(seconds: 1)); setState(() { latitude = 37.4219983; longitude = -122.084; }); } @override Widget build(BuildContext context) { return Scaffold( body: Center( child: latitude == null || longitude == null ? Text('Loading location...') : Text('Lat: $latitude, Lon: $longitude'), ), ); } }
Attempts:
2 left
💡 Hint
Think about how setState updates the UI after async data is fetched.
✗ Incorrect
The widget starts with latitude and longitude as null, so it shows 'Loading location...'. After 1 second, fetchLocation sets the values and calls setState, which rebuilds the widget to show the actual coordinates.
🧠 Conceptual
intermediate1:30remaining
Which permission is required to access GPS location on Android in Flutter?
To get the user's current GPS location in a Flutter app running on Android, which permission must be declared in the AndroidManifest.xml file?
Attempts:
2 left
💡 Hint
GPS needs precise location access.
✗ Incorrect
ACCESS_FINE_LOCATION permission allows the app to access precise location data from GPS hardware.
❓ lifecycle
advanced2:00remaining
What happens if you start listening to location updates but forget to cancel the subscription?
In Flutter, if you subscribe to continuous location updates using a StreamSubscription but do not cancel it when the widget is disposed, what is the likely outcome?
Attempts:
2 left
💡 Hint
Think about resource cleanup in widget lifecycle.
✗ Incorrect
Not cancelling a StreamSubscription causes it to keep running, leading to memory leaks and unnecessary processing.
advanced
2:30remaining
How to navigate to a screen showing a map with current location after permission granted?
You have a Flutter app that asks for location permission. After the user grants permission, you want to navigate to a MapScreen that shows the current location. Which code snippet correctly performs this navigation after permission is granted?
Flutter
Future<void> checkPermissionAndNavigate(BuildContext context) async { var status = await Geolocator.checkPermission(); if (status == LocationPermission.denied) { status = await Geolocator.requestPermission(); } if (status == LocationPermission.whileInUse || status == LocationPermission.always) { // Navigate to MapScreen } }
Attempts:
2 left
💡 Hint
You want to add the MapScreen on top without removing previous screens.
✗ Incorrect
Navigator.push adds a new screen on top of the current stack, which is appropriate after permission is granted.
📝 Syntax
expert1:30remaining
What error does this Flutter code produce when accessing location synchronously?
Consider this Flutter code snippet trying to get current location synchronously. What error will it cause?
Flutter
Position position = Geolocator.getCurrentPosition(); print('Lat: ${position.latitude}');
Attempts:
2 left
💡 Hint
Check if getCurrentPosition returns a Future or a direct value.
✗ Incorrect
getCurrentPosition() returns a Future, so assigning it directly to Position without await causes a type error.