簡體   English   中英

我怎么能帶這個詞? (撲)

[英]how can i bring this word? (flutter)

我研究 openweathermap api。

想把天氣帶到openweathermap..

API:

{"coord":{"lon":128.6811,"lat":35.2281},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50n"}],"base":"stations","main":{"temp":16.49,"feels_like":13.68,"temp_min":16,"temp_max":17,"pressure":1008,"humidity":31},"visibility":8000,"wind":{"speed":1.03,"deg":270},"clouds":{"all":90},"dt":1617013413,"sys":{"type":1,"id":5507,"country":"KR","sunrise":1616966191,"sunset":1617011007},"timezone":32400,"id":1846326,"name":"Changwon","cod":200}

我認為陰霾是天氣..

但我帶來了,它是失敗的。 我的應用程序說,'類型'String'不是'index'類型'int'的子類型'幫助plz

我的代碼:

    class TodayWide extends StatelessWidget {
  final Map weatherData;

  const TodayWide({Key key, this.weatherData}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: GridView(
        shrinkWrap: true,
        physics: NeverScrollableScrollPhysics(),
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2, childAspectRatio: 2),
        children: <Widget>[
          Todayweatherpanel(
            textColor: coyellow,
            title: '현재 온도',
            count: weatherData['main']['temp'].toString(),
          ),
          Todayweatherpanel(
            textColor: coyellow,
            title: '체감 온도',
            count: weatherData['main']['feels_like'].toString(),
          ),
          Todayweatherpanel(
            textColor: coyellow,
            title: '습도',
            count: weatherData['main']['humidity'].toString(),
          ),
          Todayweatherpanel(
            textColor: coyellow,
            title: '날씨',
            count: weatherData['weather']['main'].toString(),
          ),
        ],
      ),
    );
  }
}

用這個:

final type = weatherData['weather'][0]['main']; // prints 'Haze'

api 響應中的weather是一個數組。 如果您想訪問其中的第一個元素,請使用

final weatherMain = weatherData['weather'][0]['main'];

但最好將 json 直接轉換為 Dart class 使用Z5E056C500A1C4B06A711Zapp.c4B50D之類的工具。 這可以防止您的應用在 api 返回不同的響應時中斷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM