I want to show city name(cityName) and temperature(temp) on the flutter screen, but I can see only the red screen error, LateInitializatronError.
What I can't understand is both 'print(cityName) and print(temp)' in updateData method are work well.
It shows what that has.
How can I do?
Here is the code.
import 'package:flutter/material.dart';
class WeatherScreen extends StatefulWidget {
WeatherScreen({this.parseWeatherData});
final parseWeatherData;
@override
_WeatherScreenState createState() => _WeatherScreenState();
}
class _WeatherScreenState extends State<WeatherScreen> {
late String cityName;
late double temp;
@override
void initState() {
// TODO: implement initState
super.initState();
updateData(widget.parseWeatherData);
}
void updateData(dynamic weatherData) async {
temp = weatherData['main']['temp'];
cityName = weatherData['name'];
print(cityName);
print(temp);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'$cityName',
style: TextStyle(fontSize: 30.0),
),
SizedBox(
height: 20.0,
),
Text(
'$temp',
style: TextStyle(fontSize: 30.0),
)
],
),
),
),
);
}
}