【问题标题】:Expected a value of type 'String', but got one of the type 'Null'应为“String”类型的值,但得到“Null”类型之一
【发布时间】:2022-01-26 21:41:59
【问题描述】:
import 'package:flutter/material.dart';
import 'widgets/main_widget.dart';

import 'package:http/http.dart' as http;

import 'dart:async';

import 'dart:convert';


Future<WeatherInfo> fetchWeather() async {

  final zipCode = "180007";

  final apiKey = "d415dc7d3cae2e2b8a722dca06eb9e88";

  final requestUrl =
      "http://api.openweathermap.org/data/2.5/weather?zip={zipCode},IN&units=imperial&appid={apiKey}";


  final response = await http.get(Uri.parse(requestUrl));

  return WeatherInfo.fromJson(jsonDecode(response.body));
}


class WeatherInfo {

  final String location;

  final double temp;

  final double tempMin;

  final double tempMax;

  final String weather;

  final int Humidity;

  final double windspeed;

  WeatherInfo(

      {required this.location,

      required this.temp,

      required this.tempMin,

      required this.tempMax,

      required this.weather,

      required this.Humidity,

      required this.windspeed});

  factory WeatherInfo.fromJson(Map<String, dynamic> json) {

    return WeatherInfo(

        location: json['name'],

        temp: json['main']['temp'],

        tempMin: json['main']['temp_min'],

        tempMax: json['main']['temp_max'],

        weather: json['weather']['description'],

        Humidity: json['main']['humidity'],

        windspeed: json['wind']['speed']);

  }
}

void main() => runApp(MaterialApp(title: "Weather App", home: MyApp()));


class MyApp extends StatefulWidget {

  @override

  State<StatefulWidget> createState() {

    return _MyApp();

  }
}

class _MyApp extends State<MyApp> {

  late Future<WeatherInfo> futureWeather;

  @override

  void initState() {

    super.initState();

    futureWeather = fetchWeather();
  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

        body: FutureBuilder<WeatherInfo>(

            future: futureWeather,

            builder: (context, Snapshot) {

              if (Snapshot.hasData) {

                return MainWidget(

                    location: Snapshot.data!.location,

                    temp: Snapshot.data!.temp,

                    tempMin: Snapshot.data!.tempMin,

                    tempMax: Snapshot.data!.tempMax,

                    weather: Snapshot.data!.weather,

                    Humidity: Snapshot.data!.Humidity,

                    windspeed: Snapshot.data!.windspeed);

              } else if (Snapshot.hasError) {

                return Center(

                  child: Text("${Snapshot.error}"),

                );
              }

              return CircularProgressIndicator();
            }));
  }
}

【问题讨论】:

    标签: android flutter api dart openweathermap


    【解决方案1】:

    你应该在调用 fetchweather() 时等待

    void initState() {
    
        super.initState();
        fun();
    }
    
    void fun() async {
    futureWeather = await fetchWeather();
    setState(() {});
      }
    }
    

    【讨论】:

      【解决方案2】:

      我在您的代码中发现了两个问题:

      1. 如果您打算将 apiKey 和 zipCode 嵌入到 requestUrl 中,则必须在 {apiKey}{zipCode} 之前使用 $
      2. 如果我没记错的话,您会因为 null 安全而收到此错误,您可以通过在类型规范(例如 String?或 double? )。

      但我想如果你解决了第一个问题,那么第二个解决方案就没有必要了,因为我猜你从 API 得到错误响应,导致变量为空,所以打印你得到检查的响应并不要'不要忘记处理错误响应。

      【讨论】:

      • 谢谢您,我按照您的第一步操作,但知道它显示“预期为 'Int' 类型的值,但得到了一个 'String' 类型的值。
      • 您可以点击错误,它会突出显示导致此问题的变量,然后您就会知道要更改哪个,但是在查看您的代码时,唯一的 int 值是 Humidity,所以我猜是 API将其作为字符串返回,更改它并检查@NeerajManhas
      • 谢谢,现在可以正常使用了。再次感谢你们,这是我的第一个 Flutter 项目。
      猜你喜欢
      • 2021-11-15
      • 2021-09-18
      • 2021-06-22
      • 1970-01-01
      • 2022-10-14
      • 2021-11-02
      • 2021-12-04
      • 2021-06-06
      • 2021-10-31
      相关资源
      最近更新 更多