Reputation: 173
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
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
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