我不明白为什么会出现此错误:
我正在遵循安吉拉(Angela)的Flutter课程,并且已经与她的代码进行了核对,但我真的发现没有任何区别... widget.locationWeather
的值不为null ...我不明白为什么我会得到这个错误...感谢您的帮助,我非常感谢收到的任何帮助!
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
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:///Volumes/Mac%20Mini/App%20Development/Coding/Flutter%20Development/clima-flutter/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:4640:58)
#3 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
... Normal element mounting (4 frames)
════════════════════════════════════════════════════════════════════════════════════════════════════
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();
updateUI(widget.locationWeather);
}
void updateUI(dynamic weatherData) {
temperature = weatherData['main']['temp'];
condition = weatherData['weather'][0]['id'];
cityName = weatherData['name'];
print(temperature);
}
@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(
'32°',
style: kTempTextStyle,
),
Text(
'☀️',
style: kConditionTextStyle,
),
],
),
),
Padding(
padding: EdgeInsets.only(right: 15.0),
child: Text(
"It's ? time in San Francisco!",
textAlign: TextAlign.right,
style: kMessageTextStyle,
),
),
],
),
),
),
);
}
}
我知道updateUI
方法中的三行是有问题的,但是将温度,条件和cityName的数据类型更改为var或dynamic并没有用,并且抛出了相同的错误! / p>