Abd ur Rehman
Abd ur Rehman

Reputation: 173

The argument type 'String' can't be assigned to the parameter type 'Uri' in uri.encodeFull()

import 'package:weathertempo/model/forecastModel.dart';
import 'package:weathertempo/util/forecast_util.dart';
import 'package:http/http.dart';

class Network {
  Future<WeatherForecastModel> getWeatherForecast({String cityName}) async {
       String url =
        "http://api.openweathermap.org/data/2.5/forecast/daily?q=" +
            cityName +
            '&APPID=' +
            Util.appId +
            '&units=metric';

    // Uri.parse(finalUrl);

    final response = await get(Uri.encodeFull(url));
  }
}

The error is in 3rd last line when I tried uri.encodeFull(url) it is giving me the error stated above but when I converted the type of url to Uri it keeps giving me the same error. I also even tried uri.parse() function as shown in a comment I don't know what is the problem here if someone know feel free to answer.

Upvotes: 1

Views: 1215

Answers (2)

iDecode
iDecode

Reputation: 28896

As @jamesdlin sir mentioned, you need something like this:

Future<String> getWeatherForecast({String cityName}) async {
  final url = 'http://api.openweathermap.org/...';
  final response = await get(Uri.parse(Uri.encodeFull(url)));
  //...
}

Upvotes: 5

harsha.kuruwita
harsha.kuruwita

Reputation: 1331

You can't access url in the class definition. You can initialize it in the initState().

String url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=" +
    cityName +
    '&APPID=' +
    Util.appId +
    '&units=metric';

@override
void initState() {
  super.initState();
  var uri = Uri.encodeFull(url);
}

Use something like this way

Upvotes: 0

Related Questions