我设法从openweathermap中获取了临时数据。我设法从互联网上的这个JSON中获取了“主”数据。但是看看天气字段,描述在那里,但无法提取它。那里写着“clear”,图标为“01d”。我还将图标放入flutter中,其中有一个图像的URL,例如somelink/01d.png,我将获取并动态加载它:
{"coord":{"lon":28.9833,"lat":41.0351},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":15.87,"feels_like":15.16,"temp_min":15.04,"temp_max":17.09,"pressure":1024,"humidity":63},"visibility":10000,"wind":{"speed":3.09,"deg":120},"clouds":{"all":0},"dt":1668078936,"sys":{"type":1,"id":6970,"country":"TR","sunrise":1668055522,"sunset":1668091839},"timezone":10800,"id":745042,"name":"Istanbul","cod":200}
问题是,我有几种方法可以做到这一点。Futurebuilder在futurebuilder中,我不知道这是如何工作的。或者,从fetch函数返回一个数据数组。我不知道如何做到这一点。我完全困惑了。下面是我的简化代码:
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class TempModel {
final double temp;
final double feels_like;
final double temp_min;
final double temp_max;
final int pressure;
final int humidity;
TempModel({
required this.temp,
required this.feels_like,
required this.temp_min,
required this.temp_max,
required this.pressure,
required this.humidity,
});
factory TempModel.fromJson(Map<String, dynamic> json) {
return TempModel(
temp: json['temp'],
feels_like: json['feels_like'],
temp_min: json['temp_min'],
temp_max: json['temp_max'],
pressure: json['pressure'],
humidity: json['humidity'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
data['temp'] = temp;
data['feels_like'] = feels_like;
data['temp_min'] = temp_min;
data['temp_max'] = temp_max;
data['pressure'] = pressure;
data['humidity'] = humidity;
return data;
}
}
class WeatherModel {
final TempModel main;
WeatherModel({
required this.main,
});
factory WeatherModel.fromJson(Map<String, dynamic> json) {
return WeatherModel(
main: TempModel.fromJson(json['main']),
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
data['main'] = main;
return data;
}
}
void main() => runApp(const TestPage());
class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);
final String sehir1 = 'İstanbul';
final String sehir2 = 'Ankara';
final String sehir3 = 'İzmir';
Future<WeatherModel?> fetchWeather(String sehir) async {
//default olarak.
final http.Response response = await http.get(Uri.parse(
'https://api.openweathermap.org/data/2.5/weather?q=$sehir&units=metric&appid=666319c58bc57caab32599c61b82c50e'));
print(response.body);
print("");
try {
if (response.statusCode == 200) {
WeatherModel weatherModel =
WeatherModel.fromJson(jsonDecode(response.body));
return weatherModel;
} else {
return null;
}
} catch (e) {
log(e.toString(), name: "error");
return null;
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.orange,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Fetch Data Example'),
),
body: GridView.count(
crossAxisCount: 2,
children: <Widget>[
Expanded(
child: FutureBuilder(
future: fetchWeather(sehir1),
builder: (ctx, AsyncSnapshot<WeatherModel?> snapshot) {
if (!snapshot.hasData || snapshot.hasError) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.network(
'http://openweathermap.org/img/wn/01d.png'),
Text('$sehir1 Hava Durumu'),
Text(
'Hava sıcaklığı ${snapshot.data!.main.temp.toString()}'),
Text(
'Hissedilen sıcaklık ${snapshot.data!.main.feels_like.toString()}'),
],
),
);
}
},
),
),
],
),
),
);
}
}
1条答案
按热度按时间06odsfpq1#
weather
是一个列表:您必须使用其索引来获取值:
编辑:我发现您的模型没有考虑整个json。下面是完整的模型: