首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤动天气应用编程接口不工作snapshot.data显示错误

颤动天气应用编程接口不工作snapshot.data显示错误
EN

Stack Overflow用户
提问于 2021-09-06 07:26:05
回答 1查看 67关注 0票数 0

Main.dart

代码语言:javascript
复制
import 'package:flutter/material.dart';
import 'package:flutter_application_1/currentWeather.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CurrentWeatherPage(),
    );
  }
}

models/weather.dart

代码语言:javascript
复制
class Weather {
  final double temp;
  final double feelslike;
  final double low;
  final double high;
  final double description;

  Weather({ required this.temp, required this.feelslike, required this.low, required this.high, required this.description});

  factory Weather.fromjson(Map<String, dynamic>json) {
    return Weather(
      temp: json['main']['temp'].toDouble(),
      feelslike: json['main']['feels_like'].toDouble(),
      low: json['main']['temp_min'].toDouble(),
      high: json['main']['temp_max'].toDouble(),
      description: json['weather'][0]['description'],
    );
  }
}

currentWeather.dart

代码语言:javascript
复制
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:flutter_application_1/models/weather.dart';

class CurrentWeatherPage extends StatefulWidget{

  @override
  _CurrentWeatherPageState createState() => _CurrentWeatherPageState();
}

class _CurrentWeatherPageState extends State<CurrentWeatherPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: FutureBuilder(
          builder: (context, snapshot){
            // ignore: unnecessary_null_comparison
            if (snapshot != null){
              Weather _weather = snapshot.data;
              // ignore: unnecessary_null_comparison
              if (_weather == null){
                return Text("ERROR GETING WEATHER");
              } else {
                return weatherBox(_weather);
              }
            } else{
              return CircularProgressIndicator();
            }
          },
          future: getCurrentWeather(),
        ),
        ),
    );
  }  

  Widget weatherBox(Weather _weather){
    return Column(
      children: <Widget>[
        Text("${_weather.temp}°c"),
        Text("${_weather.description}"),
        Text("${_weather.feelslike}°c"),
        Text("H:${_weather.high}°c L:${_weather.low}°c"),
      ],
    );
  }
}

Future getCurrentWeather() async {
  Weather weather;
  String city = "chennai";
  String apikey = "safdgfdsgvf";
  var url = Uri.parse("api.openweathermap.org/data/2.5/weather?q=$city&appid=$apikey");

  final response = await http.get(url);

  if (response.statusCode == 200) {
    weather = Weather.fromjson(jsonDecode(response.body));
  }else {
    weather = Weather.fromjson(jsonDecode(response.body));
  }
   return weather;
}

在currentWeather.dart中

代码语言:javascript
复制
Weather _weather = snapshot.data;

snapshot.data显示错误:'Object?‘类型的值不能赋值给'Weather‘类型的变量。尝试更改变量的类型,或将右侧类型强制转换为“Weather”。.dart(Invalid_assignment)

如何解决此错误?

EN

回答 1

Stack Overflow用户

发布于 2021-09-06 08:01:00

您应该将Map对象的snapshot.data转换为Weather对象。

代码语言:javascript
复制
Weather _weatherl = Weather.fromjson(snapshot.data!.data());
// Output of this code is depend on how is your data structured in the database and Weather model you have defined here.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69070687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档