Bird
Raised Fist0
Fluttermobile~20 mins

Location and GPS in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of using the location feature in a Flutter app?

easy
A. To send text messages automatically
B. To change the device's screen brightness
C. To play music in the background
D. To find the device's current position on the map

Solution

  1. Step 1: Understand location feature usage

    The location feature is used to get the device's position on Earth.
  2. Step 2: Compare options with location purpose

    Only To find the device's current position on the map relates to finding the device's position, others are unrelated.
  3. Final Answer:

    To find the device's current position on the map -> Option D
  4. Quick Check:

    Location feature = Find position [OK]
Hint: Location means device position, not unrelated tasks [OK]
Common Mistakes:
  • Confusing location with other device features
  • Thinking location controls device settings
  • Assuming location sends messages or plays music
2.

Which of the following is the correct way to request location permission in Flutter using the permission_handler package?

final status = await Permission.location.____();
easy
A. ask
B. request
C. get
D. allow

Solution

  1. Step 1: Recall permission_handler syntax

    The package uses request() to ask for permission.
  2. Step 2: Match method to code snippet

    Only request fits the method call to request permission.
  3. Final Answer:

    request -> Option B
  4. Quick Check:

    Permission request method = request() [OK]
Hint: Permission requests use request(), not ask or get [OK]
Common Mistakes:
  • Using ask() instead of request()
  • Confusing get() with permission request
  • Using allow() which is not a method
3.

What will the following Flutter code print if the device's location is successfully fetched?

var location = await Geolocator.getCurrentPosition();
print('Lat: ${location.latitude}, Lon: ${location.longitude}');
medium
A. Lat: [some number], Lon: [some number]
B. Lat: 0.0, Lon: 0.0
C. SyntaxError
D. Lat: null, Lon: null

Solution

  1. Step 1: Understand getCurrentPosition()

    This method returns the device's current latitude and longitude as numbers.
  2. Step 2: Analyze print output

    The print statement outputs the latitude and longitude values, which are numbers, not null or zero by default.
  3. Final Answer:

    Lat: [some number], Lon: [some number] -> Option A
  4. Quick Check:

    getCurrentPosition() returns coordinates [OK]
Hint: getCurrentPosition() returns actual coordinates, not null [OK]
Common Mistakes:
  • Assuming coordinates are zero or null by default
  • Expecting syntax error from correct code
  • Confusing method with one that returns null
4.

Identify the error in this Flutter code snippet that tries to get location updates:

StreamSubscription positionStream = Geolocator.getPositionStream().listen((Position position) {
  print(position.latitude);
});

positionStream.cancel();
medium
A. Missing permission check before listening to location stream
B. Using listen instead of subscribe
C. Calling cancel() immediately stops the stream
D. Position type should be String, not Position

Solution

  1. Step 1: Check permission handling

    The code does not check or request location permission before starting the stream.
  2. Step 2: Review other options

    Using listen is correct; cancel() is valid but should be called later; Position type is correct.
  3. Final Answer:

    Missing permission check before listening to location stream -> Option A
  4. Quick Check:

    Permission check required before location stream [OK]
Hint: Always check permission before accessing location stream [OK]
Common Mistakes:
  • Skipping permission request before location access
  • Confusing listen() with subscribe()
  • Calling cancel() too early without reason
  • Wrong type for position parameter
5.

You want your Flutter app to continuously track the user's location but only update the UI when the user moves more than 50 meters. Which approach is best?

hard
A. Use getCurrentPosition() repeatedly every second
B. Use getPositionStream() without filters and update UI every time
C. Use getPositionStream(distanceFilter: 50) to listen for updates
D. Use getLastKnownPosition() to get location once

Solution

  1. Step 1: Understand continuous tracking with filtering

    The getPositionStream() method can listen for location changes and supports a distanceFilter to limit updates.
  2. Step 2: Compare options for efficiency

    Use getPositionStream(distanceFilter: 50) to listen for updates uses distanceFilter: 50 to update only when user moves 50 meters, saving resources and avoiding UI overload.
  3. Step 3: Eliminate other options

    Use getCurrentPosition() repeatedly every second is inefficient; C updates too often; D only gets location once.
  4. Final Answer:

    Use getPositionStream(distanceFilter: 50) to listen for updates -> Option C
  5. Quick Check:

    Use stream with distanceFilter for efficient updates [OK]
Hint: Use distanceFilter in stream to limit location updates [OK]
Common Mistakes:
  • Polling location repeatedly instead of streaming
  • Updating UI on every small location change
  • Using getLastKnownPosition for continuous tracking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard