Fix the next step for a location request

Review the AI's codeEasy10 minGoogle style

Your task

An AI wrote a Flutter "find nearby" feature that called requestPermission(), ignored the result, and went straight to getCurrentPosition(). On a phone where location was already allowed, it worked. After a tester denied the prompt, or with location switched off, the feature hung or crashed. The fix turns the permission result into one next step.

An assistant wrote resolve_location_request(permission_status, service_enabled, has_shown_rationale). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • permission_status: the status the permission API returned.
  • service_enabled: location services are on at the operating system level.
  • has_shown_rationale: the app has already explained once why it needs location.
  • Return the next step: fetch, request_permission, show_rationale, open_app_settings, open_location_settings or unsupported.

Rules the tests check. Return the first that applies:

  1. The status is not exactly one of notDetermined, denied, deniedForever, restricted, whileInUse or always, case included: unsupported.
  2. deniedForever or restricted: open_app_settings, since the app can never ask again.
  3. Location services are off: open_location_settings.
  4. whileInUse or always: fetch. This is the only step that reads the position.
  5. notDetermined: request_permission.
  6. denied: request_permission if the rationale has been shown, otherwise show_rationale.

Examples

resolve_location_request(permission_status: str, service_enabled: bool, has_shown_rationale: bool) -> str
Example 1
Inputresolve_location_request("whileInUse", True, False)
Output"fetch"
Example 2
Inputresolve_location_request("denied", True, False)
Output"show_rationale"
Example 3
Inputresolve_location_request("deniedForever", True, True)
Output"open_app_settings"