Json 类型';字符串';不是类型为';int';属于';指数';从openweathermap获取位置API时

Json 类型';字符串';不是类型为';int';属于';指数';从openweathermap获取位置API时,json,flutter,dart,Json,Flutter,Dart,我在尝试一个关于颤振课程()的教程,其中位置数据从API调用中检索,错误在解码数据时出现, 我尝试更改变量的数据类型,但错误仍然存在。 错误消息是: The following _TypeError was thrown building Builder: type 'String' is not a subtype of type 'int' of 'index' The relevant error-causing widget was: MaterialApp file:///C:

我在尝试一个关于颤振课程()的教程,其中位置数据从API调用中检索,错误在解码数据时出现, 我尝试更改变量的数据类型,但错误仍然存在。 错误消息是:

The following _TypeError was thrown building Builder:
type 'String' is not a subtype of type 'int' of 'index'

The relevant error-causing widget was: 
  MaterialApp file:///C:/Users/saleem/StudioProjects/Clima/lib/main.dart:9:12
When the exception was thrown, this was the stack: 
#0      _LocationScreenState.updateUI (package:clima/screens/location_screen.dart:25:30)
#1      _LocationScreenState.initState (package:clima/screens/location_screen.dart:21:5)
#2      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
#3      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
...     Normal element mounting (132 frames)
我试着将变量的数据类型改为全字符串和全变量,我真的很喜欢编程场景 我将在下面附上dart代码:

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

class LocationScreen extends StatefulWidget {
  LocationScreen({this.locationWeather});
  final locationWeather;

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

class _LocationScreenState extends State<LocationScreen> {
  double temperature;
  int condition;
  String cityName;

  @override
  void initState() {
    super.initState();
    print(widget.locationWeather);
    updateUI(widget.locationWeather);
  }

  void updateUI(dynamic weatherData) {
    temperature = weatherData['main']['temp'];
    condition = weatherData['weather'][0]['id'];
    cityName = weatherData['name'];
  }

  @override

  Widget build(BuildContext context) {
    return Scaffold(
      ...
    );
  }
}
问题解决了我使用了JSONECODE而不是jsonDecode


链接到教程会很好。我想是的

condition = weatherData['weather'][0]['id'];
应该是哪一个

condition = weatherData['weather']['0']['id'];

int条件=>字符串条件;
或者将第30行解析为int

API输出是一个字符串

首先导入'dart:convert'

并将您的API输出包装在jsonDecode(API_输出)


这将使它成为一个json对象,其内容可以通过您正在执行的方式访问。

不,这不起作用。请添加api的输出,这样会更容易提供帮助。I/flatter(14299):“{“coord\”:{“lon\”:139.01,\“lat\”:35.02},\“weather\”:[{“id\”:800,\“main\”:“Clear\”,“description\:“Clear sky\”,“icon\:“01n\”,”“压力::1013.75,”湿度::100,“温度最低点”温度:100,“温度最低点”温度:::285.514,“温度最低点”温度:285.514,“温度最高点”温度:285.514,“温度最高点”温度:285.514,“温度最高点”温度:285.514,“海平面”海平面:285.514,“海平面”海平面:1023.22,“GRNDU水平”水平:1013.22,“GRNDU水平”水平:1013.75},,“风”风”风::::::::,,,,:::::::,“风::::::“[速度:“速度:速度:速度::5.5.52,:5.52,,,,,,,,,,,,,,“温度:“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,“JP\”,“日出”:1485726240,“日落”:1485763863},“id\“:1907296,\“name\”:\“Tawarano\”,“cod\”:200}“不起作用我试着注释我解码json的行,并将变量类型更改为StringcityName=weatherData['name'].toString();我试过了,但没有效果,jsonDecode(weatherData)??
condition = weatherData['weather']['0']['id'];