0
0
Fluttermobile~20 mins

Location and GPS in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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'),
      ),
    );
  }
}
AText widget showing 'Lat: 0.0, Lon: 0.0' after 1 second
BText widget showing 'Loading location...' initially, then 'Lat: 37.4219983, Lon: -122.084' after 1 second
CText widget showing 'Lat: null, Lon: null' immediately and never updates
DApp crashes due to null error when accessing latitude and longitude
Attempts:
2 left
💡 Hint
Think about how setState updates the UI after async data is fetched.
🧠 Conceptual
intermediate
1: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?
A<uses-permission android:name="android.permission.INTERNET" />
B<uses-permission android:name="android.permission.READ_CONTACTS" />
C<uses-permission android:name="android.permission.CAMERA" />
D<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Attempts:
2 left
💡 Hint
GPS needs precise location access.
lifecycle
advanced
2: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?
AThe app will crash immediately when the widget is disposed
BThe subscription automatically cancels when the widget is disposed, so no problem occurs
CThe app will leak memory and continue receiving location updates even after the widget is gone
DThe location updates will stop but the app will freeze
Attempts:
2 left
💡 Hint
Think about resource cleanup in widget lifecycle.
navigation
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
  }
}
ANavigator.push(context, MaterialPageRoute(builder: (_) => MapScreen()));
BNavigator.pop(context); Navigator.pushNamed(context, '/map');
CNavigator.pushReplacement(context, MaterialPageRoute(builder: (_) => MapScreen()));
DNavigator.pushNamedAndRemoveUntil(context, '/map', (route) => false);
Attempts:
2 left
💡 Hint
You want to add the MapScreen on top without removing previous screens.
📝 Syntax
expert
1: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}');
ATypeError: A Future was used where a Position was expected
BSyntaxError: Missing semicolon
CNo error, prints latitude immediately
DRuntimeError: Location services disabled
Attempts:
2 left
💡 Hint
Check if getCurrentPosition returns a Future or a direct value.