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() 函数,如评论中所示,如果有人知道可以随时回答,我不知道这里有什么问题。
答案 0 :(得分:1)
正如 @jamesdlin 先生所说,您需要这样的东西:
Future<String> getWeatherForecast({String cityName}) async {
final url = 'http://api.openweathermap.org/...';
final response = await get(Uri.parse(Uri.encodeFull(url)));
//...
}
答案 1 :(得分:0)
您无法在类定义中访问 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);
}
像这样使用