Home > Blockchain >  Flutter: Unhandled Exception: type 'Future<List<dynamic>>' is not a subtype of
Flutter: Unhandled Exception: type 'Future<List<dynamic>>' is not a subtype of

Time:09-18

I'm getting the error

Unhandled Exception: type 'Future<List<dynamic>>' is not a subtype of type 'List<dynamic>' in type cast

I am unable to get past this issue. I'm trying to learn Bloc and using Api's with Bloc by building a weather app. I have encountered this issue and have been unable to get past it.

What could it be and how can I get past it?

Thanks for any help.

My code is below:

class GetWeatherService {
  List get coordinatesList => [];

  //CHECK AND GET LOCATION PERMISSION
  Future<List> checkAndGetLocationPermissions() async {
    // Check if gps is enabled
    bool servicestatus = await Geolocator.isLocationServiceEnabled();
    if (servicestatus) {
      if (kDebugMode) {
        print("GPS service is enabled");
      }
    } else {
      if (kDebugMode) {
        print("GPS service is disabled.");
      }

      // Check for and request location permission
      LocationPermission permission = await Geolocator.checkPermission();

      if (permission == LocationPermission.denied) {
        permission = await Geolocator.requestPermission();
        if (permission == LocationPermission.denied) {
          if (kDebugMode) {
            print('Location permissions are denied');
          }
        } else if (permission == LocationPermission.deniedForever) {
          if (kDebugMode) {
            print("Location permissions are permanently denied");
          }
        } else {
          if (kDebugMode) {
            print("GPS Location service is granted");
          }
        }
      } else {
        if (kDebugMode) {
          print("GPS Location permission granted.");
        }
        Position position = await Geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.high);
        dynamic latitude = position.latitude;
        dynamic longitude = position.longitude;
        List coordinatesList = [latitude, longitude];
        if (kDebugMode) {
          print(coordinatesList);
        }
      }
    }
    return coordinatesList;
  }

  Future<GetWeatherDetails> getWeather() async {
    List latLong = checkAndGetLocationPermissions() as List;
    // Future<String> placeName = translateLatLongToPlace();
    double latitude = latLong[0];
    double longitude = latLong[1];

    final response = await http.get(Uri.parse(
        "api.openweathermap.org/data/2.5/forecast?lat=$latitude&lon=$longitude&appid=$openWeatherMapApiKey"));
    final weatherDetails = getWeatherDetailsFromJson(response.body);
    return weatherDetails;
  }
}

CodePudding user response:

Use await when you are taking value from a future function.

Future<GetWeatherDetails> getWeather() async {
    List latLong = await checkAndGetLocationPermissions() as List;
    ... 
  }
  • Related