Home > Software design >  Error when decoding Uri.https, "Unhandled Exception: FormatException: Invalid character (at cha
Error when decoding Uri.https, "Unhandled Exception: FormatException: Invalid character (at cha

Time:04-16

I'm trying to built weather app. I've searched weather API's, because I need make hourly and weekly weather forecast. I found this website https://www.visualcrossing.com/. I want to decode HTTP link (below in code) like this:

class DataServiceOneCall {
  Future<WeatherResponseOnecall> getWeatherOneCall(String city) async {
    //https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Olszowice?unitGroup=metric&key=QVDLJFCCQRM5296M7J975FP2W&contentType=json

    final queryParameters = {
      'q': city,
      'unitGroup': 'metric',
      'key': 'QVDLJFCCQRM5296M7J975FP2W',
      'contentType': 'json',
    };

    final uri = Uri.https(
        'weather.visualcrossing.com/VisualCrossingWebServices',
        '/rest/services/timeline,',
        queryParameters);

    final response_onecall = await http.get(uri);

    print(response_onecall.body);

    final json = jsonDecode(response_onecall.body);
    return WeatherResponseOnecall.fromJson(json);
  }
}

And I've got error thrown like this.

E/flutter (11332): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Invalid character (at character 27)
E/flutter (11332): weather.visualcrossing.com/VisualCrossingWebServices
E/flutter (11332):                           ^
E/flutter (11332):
E/flutter (11332): #0      _Uri._fail (dart:core/uri.dart:1754:5)
E/flutter (11332): #1      _Uri._normalizeRegName (dart:core/uri.dart:2252:9)
E/flutter (11332): #2      _Uri._makeHost (dart:core/uri.dart:2113:12)
E/flutter (11332): #3      new _Uri (dart:core/uri.dart:1640:12)
E/flutter (11332): #4      _Uri._makeHttpUri (dart:core/uri.dart:1813:12)
E/flutter (11332): #5      new _Uri.https (dart:core/uri.dart:1678:12)
E/flutter (11332): #6      DataServiceOneCall.getWeatherOneCall package:weather_app/data_service/data_service_onecall.dart:18

Thanks for help.

CodePudding user response:

I rewrite this code like this and it works.

final uri = Uri.https(
        'weather.visualcrossing.com',
        '/VisualCrossingWebServices/rest/services/timeline/${city}?',
        queryParameters);

CodePudding user response:

V after slash would be small.

final uri = Uri.https(
    'weather.visualcrossing.com/visualCrossingWebServices',
    '/rest/services/timeline,',
  • Related