无法将参数类型 'String' 分配给 uri.encodeFull() 中的参数类型 'Uri'

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));
  }
}

当我尝试 uri.encodeFull(url) 时,错误出现在倒数第三行,它给了我上述错误,但是当我将 url 的类型转换为 Uri 时一直给我同样的错误。我什至还尝试了 uri.parse() 功能,如评论中所示我不知道这里有什么问题,如果有人知道可以随时回答。

您无法访问 class 定义中的 url。你可以 initialize 它在 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);
}

像这样使用

正如@jamesdlin先生所说,您需要这样的东西:

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