'Try correcting the name to the name of an existing method, or defining a method named 'fetchCity'
In order to get weather information about an entered city, I Created a fetch city function as bellow :
class CityModel {
final String? name;
final String? lat;
final String? lon;
var cityJSON;
CityModel({
this.name,
this.lat,
this.lon});
String get city{
return city;
}
Future<CityModel?> fetchCity(String cityName) async{
if (cityJSON == null) {
var link ="https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/cities.json";
var response = await http.get(Uri.parse(link));
if (response.statusCode == 200) {
cityJSON = json.decode(response.body);
}
}
for (var i = 0; i < cityJSON.length; i++) {
if (cityJSON[i]["name"].toString().toLowerCase() == cityName.toLowerCase()) {
return CityModel(
name: cityJSON[i]["name"].toString(),
lat: cityJSON[i]["latitude"].toString(),
lon: cityJSON[i]["longitude"].toString(),
);
}
}
return null;
}
}
then, I tried to call it in another file as shown bellow :
class CurrentWeather extends StatefulWidget {
final Function() updateData;
CurrentWeather(this.updateData);
@override
State<CurrentWeather> createState() => _CurrentWeatherState();
}
class _CurrentWeatherState extends State<CurrentWeather> {
bool searchBar = false;
bool updating = false;
var focusNode = FocusNode();
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: (){
if(searchBar)
setState(() {
searchBar = false;
});
},
child: GlowContainer(
height: MediaQuery.of(context).size.height - 250,
padding: EdgeInsets.only(top: 20, left: 30, right: 30, bottom: 20),
margin: EdgeInsets.all(2),
glowColor: Color(0xff00A1FF).withOpacity(0.8),
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(60),
bottomRight: Radius.circular(60),
),
color: Color(0xff00A1FF),
spreadRadius: 5,
child: Column(
children: [
Container(
///////////// If the user click the Search bar
child: searchBar?
TextField(
focusNode: focusNode,
decoration: InputDecoration(
border: OutlineInputBorder(borderRadius: BorderRadius.circular(10),),
fillColor: Color(0xff030317),
filled: true,
hintText: "Enter a city Name",
),
textInputAction: TextInputAction.search,
onSubmitted: (value) async{
//My call is here //////////
CityModel temp = await fetchCity(value);
if (temp == null) {
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
backgroundColor: Color(0xff030317),
title: Text("City not found "),
content: Text("Please check the city name"),
actions: [
TextButton(
onPressed: (){
Navigator.of(context).pop();
},
child: Text("ok")),
],
);
}
);
searchBar = false;
return;
}
city = temp.city;
lat = temp.lat!;
lon = temp.lon!;
updating = true;
setState(() {
});
widget.updateData;
searchBar = false;
updating = false;
setState(() {
});
},
)
: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(CupertinoIcons.square_grid_2x2,
color: Colors.white,
),
Row(
children: [
Icon(CupertinoIcons.map_fill, color: Colors.white,),
GestureDetector(
onTap: (){
searchBar = true;
setState(() {
});
focusNode.requestFocus();
},
child: Text("" + city, style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 30,
),
),
),
],
),
Icon(Icons.more_vert, color: Colors.white,),
],
),
),
Container(
margin: EdgeInsets.only(top: 10),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
border: Border.all(width: 0.2, color: Colors.white),
borderRadius: BorderRadius.circular(30),
),
child: Text(
updating ? 'updating' : 'updated', style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
Container(
height: 300,
child: Stack(
children: [
Image(
width:200 ,
height:200 ,
image: AssetImage(currentTemp!.image!),
fit: BoxFit.fill,
),
SizedBox(height: 10,),
Positioned(
bottom: 0,
right: 0,
left: 0,
child:Center(
child: Column(
children: [
GlowText(currentTemp!.current!.toString(),
style: TextStyle(
fontSize: 100,
fontWeight: FontWeight.bold,
),
),
Text(currentTemp!.name!,
style: TextStyle(fontSize: 20,),
),
Text(currentTemp!.day!,
style: TextStyle(fontSize: 18,),
),
],
),
),
),
],
),
),
Divider(color: Colors.white,),
SizedBox(height: 5,),
ExtraWeather(currentTemp!),
],
),
),
);
}
}
I'm getting this error:
The method 'fetchCity' isn't defined for the type '_CurrentWeatherState'. Try correcting the name to the name of an existing method, or defining a method named 'fetchCity'. in Advance thank you for your help
Solution 1:[1]
Since you declared your method inside CityModel you have to create an instance of it to call fetchCity.
final CityModel cityModel = CityModel();
final CityModel? city = awaitfetchCity('MyCity');
If you don't want to create an instance of CityModel you can either make fetchCity a global method or a static.
static Future<CityModel?> fetchCity(String cityName) async{ ... }
// Call the method
final CityModel? city = await CityModel.fetchCity('MyCity');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | quoci |
