【发布时间】:2021-07-29 08:59:42
【问题描述】:
我正在获取数据 api,并从 Json 转换它。 我有这种方法来获取数据,打赌在未来的构建器中使用它时它会给我 getPData() 方法在 null 上调用。
这是我创建 getPData 方法的地方:
class JsonConnection {
JsonConnection.jsonDecode();
static double pLat;
static double pLong;
PData timesList;
Future getPData() async {
final pos = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.medium);
pLat = pos.latitude;
pLong = pos.longitude;
String date = DateTime.now().toString();
int method = 4;
final url = Uri.parse(
'http://api.aladhan.com/v1/timings/$date?latitude=$pLat&longitude=$pLong&method=$method');
http.Response res = await http.get(url);
var data = json.decode(res.body);
timesList = PData.fromJson(data);
return timesList;
}
这里是我使用 getPData 的地方:
body: FutureBuilder(
future: jsonConnection.getPData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
print("hello");
print(snapshot.data.meta.timezone);
if (snapshot.hasData) {
CircleAvatar();
return Center(
child: Container(
padding: const EdgeInsets.only(top: 150.0),
child: Column(
children: [
Table(
border: TableBorder.all(color: Colors.purpleAccent),
defaultColumnWidth: FixedColumnWidth(150.0),
defaultVerticalAlignment:
TableCellVerticalAlignment.middle,
children: <TableRow>[
TableRow(
children: <Widget>[
TableCell(
verticalAlignment:
TableCellVerticalAlignment.middle,
child: Center(
child: Text(snapshot.data.meta.timezone,
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold)),
)),
我能做些什么来解决它?
【问题讨论】: