flutter - 仅在热重新加载后才加载我的值
问题描述
我一直在开发一个显示实时天气数据的天气应用程序。我对颤振和飞镖相对较新。
当我运行代码时,应用程序构建,而不是在屏幕上显示三个值,所有三个值都显示为 null。但是当我热重载时,值会出现。任何人?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'Services/LocationServices.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
var Longitude;
var Latitude;
var CityName;
var Temperature;
var Description;
var appid = '111267e28d8012bc99ae1aa67bb9d87f';
void getLocation() async{
Location location = Location();
await location.getCurrentLocation();
Latitude = location.latitude;
Longitude = location.longitude;
print('Latitude = ${location.latitude}');
print('Longitude = ${location.longitude}');
getData();
}
void getData() async{
var url = Uri.parse('https://api.openweathermap.org/data/2.5/weather?lat=$Latitude&lon=$Longitude&appid=$appid&units=metric');
http.Response response = await http.get(url);
if (response.statusCode == 200){
String Data = response.body;
var city = jsonDecode(Data)['name'];
CityName = city;
var temp = jsonDecode(Data)['main']['temp'];
Temperature = temp;
var condition = jsonDecode(Data)['weather'][0]['description'];
Description = condition;
// print('City = $city');
// print('Temperature = $temp');
// print('Description = $condition');
}else{
print(response.statusCode);
}
}
@override
void initState() {
super.initState();
getLocation();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(child: Text('$CityName')),
Center(child: Text('$Temperature')),
Center(child: Text('$Description')),
// Text('$temperature'),
// Text('$temperature'),
// Text('$temperature'),
],
)
),
);
}
}
这是 LocationServices.dart 文件。有人对我在这里做错了什么有任何想法吗?
import 'package:geolocator/geolocator.dart';
class Location{
var latitude;
var longitude;
Future<void> getCurrentLocation() async{
try{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
latitude = position.latitude;
longitude = position.longitude;
}
catch(e){
print(e);
}
}
}
解决方案
您需要调用setState
以触发状态更改StatefulWidget
void getData() async {
var url = Uri.parse(
'https://api.openweathermap.org/data/2.5/weather?lat=$Latitude&lon=$Longitude&appid=$appid&units=metric');
http.Response response = await http.get(url);
if (response.statusCode == 200) {
setState(() {
String Data = response.body;
var city = jsonDecode(Data)['name'];
CityName = city;
var temp = jsonDecode(Data)['main']['temp'];
Temperature = temp;
var condition = jsonDecode(Data)['weather'][0]['description'];
Description = condition;
});
// print('City = $city');
// print('Temperature = $temp');
// print('Description = $condition');
} else {
print(response.statusCode);
}
}
推荐阅读
- javascript - 用换行符保存的 textarea 无法按创建返回到我的项目的前端,如何解决?
- excel - 为什么选择范围变量时会发生变化?
- php - 如何使用 php 和 mysql 变量在 html 表中设置数据
- python - Pandas 中数据框列的索引
- flutter - 我们可以在 Flutter 中创建一个新的小部件吗?
- android - 尝试在空对象引用 NFC 上调用虚拟方法“boolean java.lang.String.equals(java.lang.Object)”
- reactjs - 如何在样式化组件中访问 Material-ui 的主题
- java - 根据SQL查询传入的参数选择WHERE后面的部分
- amazon-web-services - 具有两个负载均衡器用于同一端口的 ECS 服务:内部和面向 Internet
- apache-spark - 如何在 PySpark 中的数据框列中转换 JSON 字符串?