【问题标题】:type string is not a subtype of type int in flutter类型 string 不是颤振中 int 类型的子类型
【发布时间】:2021-02-25 02:10:06
【问题描述】:

这段代码我有这个问题,我尝试将变量转换为动态但没有成功,希望有人知道解决方案,谢谢请帮助。

这段代码我有这个问题,我尝试将变量转换为动态但没有成功,希望有人知道解决方案,谢谢请帮助。

import 'package:flutter/material.dart';
import 'package:clima/utilities/constants.dart';
import 'package:clima/services/weather.dart';

class LocationScreen extends StatefulWidget {
  LocationScreen({this.locationWeather});
  final locationWeather;
  @override
  _LocationScreenState createState() => _LocationScreenState();
}

class _LocationScreenState extends State<LocationScreen> {
  WeatherModel weather = WeatherModel();
  int temperature;
  String weatherIcon;
  String cityName;
  String weatherMessage;
  @override
  void initState() {
    // ignore: todo
    // TODO: implement initState
    super.initState();
    updateUI(widget.locationWeather);
  }

  // ignore: non_constant_identifier_names
  void updateUI(dynamic WeatherData) {
    setState(() {
      temperature = WeatherData['main']['temp'];

      var condition = WeatherData['weather'][0]['description'];

      weatherIcon = weather.getWeatherIcon(condition);

      weatherMessage = weather.getMessage(temperature);

      cityName = WeatherData['name'];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('images/location_background.jpg'),
            fit: BoxFit.cover,
            colorFilter: ColorFilter.mode(
                Colors.white.withOpacity(0.8), BlendMode.dstATop),
          ),
        ),
        constraints: BoxConstraints.expand(),
        child: SafeArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  FlatButton(
                    onPressed: () {},
                    child: Icon(
                      Icons.near_me,
                      size: 50.0,
                    ),
                  ),
                  FlatButton(
                    onPressed: () {},
                    child: Icon(
                      Icons.location_city,
                      size: 50.0,
                    ),
                  ),
                ],
              ),
              Padding(
                padding: EdgeInsets.only(left: 15.0),
                child: Row(
                  children: <Widget>[
                    Text(
                      '$temperature°',
                      style: kTempTextStyle,
                    ),
                    Text(
                      weatherIcon,
                      style: kConditionTextStyle,
                    ),
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.only(right: 15.0),
                child: Text(
                  '$weatherMessage as  in $cityName',
                  textAlign: TextAlign.right,
                  style: kMessageTextStyle,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 这是 Java 还是 Javascript?一个本质上是一个玩具,专为编写小段代码而设计,传统上被缺乏经验的程序员使用和滥用。另一种是用于网络浏览器的脚本语言。
  • 嘿,主要是你的模特。您可能会为 WeatherData 中的“临时”键返回整数。但在您也发布您的模型之前,无法确认。
  • @Pushpendra 检查这个codeshare.io/24r69L
  • 您的 WeatherData (我假设为 json)似乎提供了一个字符串而不是一个 int 来表示温度和/或条件。尝试将 int.parse(condition) 和 int.parse(temperature) 传递给 WeatherModel。

标签: javascript java flutter dart


【解决方案1】:

试试这个,

  void updateUI(dynamic weatherData) {
    setState(() {
      if (weatherData == null) {
        temperature = 0;
        weatherIcon = 'Error';
        weatherMessage = 'Unable to get weather data';
        cityName = '';
        return;
      }
      double temp = weatherData['main']['temp'];
      temperature = temp.toInt();
      var condition = weatherData['weather'][0]['id'];
      weatherIcon = weather.getWeatherIcon(condition);
      weatherMessage = weather.getMessage(temperature);
      cityName = weatherData['name'];
    });
  }

【讨论】:

    【解决方案2】:

    当您从地图访问数据时,您需要明确告诉它是一个 int ,尝试将 WeatherData['weather'][0]['description'] 转换为 WeatherData['weather'][0]['description'] as int 。此外,如果任何数据可以为空,您还需要使用问号 int? 明确说明这一点,不确定您的模型 WeatherData 是否允许这样做。

        setState(() {
          temperature = WeatherData['main']['temp']  as int ;
    
          var condition = WeatherData['weather'][0]['description'];
    
          weatherIcon = weather.getWeatherIcon(condition);
    
          weatherMessage = weather.getMessage(temperature);
    
          cityName = WeatherData['name'];
        });
      }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2019-08-14
      • 2020-08-11
      • 1970-01-01
      • 2021-08-12
      • 2020-09-29
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多