I get this error:
Const variables must be initialized with a constant value.
Try changing the initializer to be a constant expression
My code:
Future<void> _fetchData(String city) async {
const apiURL =
'http://api.weatherapi.com/v1/current.json?key=(secret)=$city&aqi=no';
I get the error at the $city
Anyone can help?
CodePudding user response:
here you esentially doing string interpolation, hence your apiURL is dynamic, not const. use final instead
Future<void> _fetchData(String city) async {
final apiURL =
'http://api.weatherapi.com/v1/current.json?key=(secret)=$city&aqi=no';
CodePudding user response:
Since you are using $city (which receives the data from the argument), you can no longer define it as a const.
const variables are those whose values are pre-determined and do not change over time. e.g., http://api.weatherapi.com/v1/current.json can be a const but http://api.weatherapi.com/v1/current.json?key=(secret)=$city&aqi=no cannot be a const.
Simply use final (recommended) or var and It should work just fine.