Skip to content

[Bug]: _TypeError during toString() of the exception itself #276

@baqwas

Description

@baqwas

Please check the following before submitting a new issue.

Please select affected platform(s)

  • Android
  • iOS

Steps to reproduce

Using v2.2.1 on the following snippet:


The location string being passed is "London, UK"

Workaround for beginner level usage of flutter-geocoding would be appreciated. Thanks.

Regards.

Expected results

Expecting some viewable reason or code for the inability to return the geocoordinates.

Actual results

The error message on the Snackbar is now as follows: Details: Generic Error - Type: _TypeError, Message: Null check operator used on a null value

Code sample

Code sample
try {
      List<Location> locations = await locationFromAddress(locationQuery);

      if (locations.isNotEmpty) {
        final Location firstLocation = locations.first;
        setState(() {
          _latitudeController.text = firstLocation.latitude.toString();
          _longitudeController.text = firstLocation.longitude.toString();
        });
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('Found coordinates: Lat ${firstLocation.latitude}, Lon ${firstLocation.longitude}')),
        );
      } else {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Location not found. Try a different query or check your internet connection.')),
        );
      }
    } on PlatformException catch (e) {
      String errorMessage;
      if (e.code == 'PERMISSION_DENIED') {
        errorMessage = 'Location permission denied. Please grant permission in app settings (or check desktop portals).';
      } else if (e.code == 'SERVICE_NOT_AVAILABLE') {
        errorMessage = 'Location service not available on device or network error. (e.g., geoclue not running on Linux)';
      } else if (e.message != null && e.message!.contains('Failed to get locations')) {
        errorMessage = 'Could not get locations. Ensure internet connection and correct location string. Error: ${e.message}';
      } else {
        errorMessage = 'Platform error fetching coordinates: ${e.message ?? e.code}. Type: ${e.runtimeType}';
      }
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text(errorMessage)),
      );
    } catch (e) {
      String errorMessage = 'An unexpected error occurred while fetching coordinates.';
      String errorDetails = '';

      if (e is PlatformException) {
        // If it is a PlatformException, try to get its code and message safely
        errorDetails = 'Platform Error - Code: ${e.code ?? 'N/A'}, Message: ${e.message ?? 'N/A'}.';
      } else {
        // For other types of exceptions, try to get string representation safely
        try {
          errorDetails = 'Generic Error - Type: ${e.runtimeType}, Message: ${(e as Object?).toString()}';
        } catch (e2) {
          // Fallback if even the safe toString() fails
          errorDetails = 'Generic Error - Type: ${e.runtimeType}, Failed to get message string: ${e2.toString()}';
        }
      }
 

Screenshots or video

Screenshots or video demonstration

[Upload media here]

Version

2.2.1

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.32.5, on Ubuntu 25.04 6.14.0-15-generic, locale en_US.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2025.1.1)
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions