I have seen answers on the internet but in all of them the OP has used StatelessWidget rather than StatefulWidget. i have used a StatefulWidget but still setState() is not recognized
Kindly refer to line 43 of the code.
Hi! i am trying to learn flutter and was making a Weather forecast app as a project
in order to achieve that i was following a tutorial on youtube the youtuber had no error as he was working in visual studio code, I on the other am using Android studio
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(
MaterialApp(
title: "Weather App",
home: Home(),
)//MaterialApp
);
class Home extends StatefulWidget {
@override
State<StatefulWidget>createState()
{
return _HomeState();
}
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}
}
class _HomeState extends State<Home>{
var temp;
var description;
var currently;
var humidity;
var windSpeed;
Future getWeather() async {
http.Response response = await http.get(
"https://api.openweathermap.org/data/2.5/weather?q=Delhi&APPID=4570e05ab53aad5b3893831a809ee608");
var results = jsonDecode(response.body);
setState((){ //<- THIS IS NOT RECOGNIZED
this.temp = results['main']['temp'];
this.description = results['weather'][0]['description'];
this.currently = results['weather'][0]['main'];
this.humidity = results['main']['humidity'];
this.windSpeed = results['main']['speed'];
})
}
@override
Widget build (BuildContext context)
{
return Scaffold(
body: Column(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height / 3,
width: MediaQuery.of(context).size.width,
color: Colors.red,
child: Column(
mainAxisAlignment: MainAxisAlignment.center ,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
"Currently in Boston",
style: TextStyle
(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.w600
),
),
),
Text(
"52u00B0",
style:TextStyle(
color: Colors.white,
fontSize:40.0,
fontWeight: FontWeight.w600
),
),
Padding(
padding: EdgeInsets.only(bottom: 10.0),
child: Text(
"Rain",
style: TextStyle
(
color: Colors.white,
fontSize: 14.0,
fontWeight: FontWeight.w600
),
),
),
],
),
),
Expanded(child: Padding(
padding: EdgeInsets.all(20.0),
child:ListView(
children: <Widget>[
ListTile(
leading: FaIcon(FontAwesomeIcons.temperatureHalf),
title: Text("Temprature"),
trailing: Text("52u00B0"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.cloud),
title: Text("Weather"),
trailing: Text("Weather"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.sun),
title: Text("Humidity"),
trailing: Text("12"),
),
ListTile(
leading: FaIcon(FontAwesomeIcons.wind),
title: Text("Wind Speed"),
trailing: Text("12"),
),
],
)
))
],
),
);
}
}
3
Answers
Fixed it! was missing a semicolon.
I think you need to call your function getWeather, for example in the init state method.
There is a
;
missing at the end of your setState((){…});
I think you should use
Uri.parse()
: