我不认为我与供应商打交道很好。什么是正确的代码?
**import 'package:flutter/material.dart';
import 'package:geo_app/model/wether.dart';**
class UpdateWeather extends ChangeNotifier{
WeatherModel weathers = WeatherModel();
int temperature;
dynamic weatherImage;
String city;
dynamic weatherBack;
Future<void> updateUi(dynamic weatherData) async {
double temp =await weatherData['main']['temp'].toDouble();
temperature = temp.toInt();
var condition =await weatherData['weather'][0]['id'];
weatherBack = weathers.getWBackgroundImage(condition);
weatherImage = weathers.getWeatherImage(condition);
city = weatherData['name'];
notifyListeners();
}
}
错误信息是这样的。
当继承的小部件发生变化时,例如,如果 Theme.of() 的值发生变化,则重新构建其依赖的小部件。如果依赖小部件对继承小部件的引用在构造函数或 initState() 方法中,则重建的依赖小部件将不会反映继承小部件中的更改。
通常,对继承的小部件的引用应该出现在小部件 build() 方法中。或者,可以将基于继承的小部件的初始化放在 didChangeDependencies 方法中,该方法在 initState 之后以及此后依赖项发生更改时调用。
相关的导致错误的小部件是
MaterialApp
lib/main.dart:15
抛出异常时,这是堆栈
这是使用此提供程序的代码。
import 'package:flutter/material.dart';
import 'package:geo_app/Provider/update_wearher.dart';
import 'package:geo_app/const.dart';
import 'package:geo_app/model/wether.dart';
import 'package:geo_app/view/navigator_dawer.dart';
import 'package:provider/provider.dart';
class LocationScreen extends StatefulWidget {
static const routeName = '/lacation';
final locationWeather;
const LocationScreen({Key key, this.locationWeather}) : super(key:
key);
@override
_LocationScreenState createState() => _LocationScreenState();
}
class _LocationScreenState extends State<LocationScreen> {
WeatherModel weathers = WeatherModel();
@override
void initState() {
super.initState();
Provider.of<UpdateWeather>
(context).updateUi(widget.locationWeather);
}
@override
void setState(fn) {
Provider.of<UpdateWeather>
(context).updateUi(widget.locationWeather);
super.setState(fn);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body:Stack(children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.grey[850],
image: DecorationImage(
image: AssetImage(Provider.of<UpdateWeather>
(context).weatherBack),
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.5), BlendMode.dstATop),
fit: BoxFit.cover),
),
constraints: BoxConstraints.expand(),
padding: EdgeInsets.only(top : 200),
child: Column(children: <Widget>[
Image.asset(Provider.of<UpdateWeather>
(context).weatherImage,
height: 120,
width:120,
),
// Image.asset(
// 'assets/sun.png',
// alignment: Alignment.center,
// height: 120,
// width:120,
// ),
SizedBox(height:30),
Text('${Provider.of<UpdateWeather>(context).temperature}°',
style: ktextStyle,
),
SizedBox(
height:10
),
Text('${Provider.of<UpdateWeather>(context).city}',
style: ktextStyle,)
],
),
),
Positioned(
child:AppBar(
actions: <Widget>[
IconButton(icon: Icon(Icons.near_me),
onPressed: () async {
var weatherData = await weathers.getLocationWeather();
Provider.of<UpdateWeather>(context).
updateUi(weatherData);
})
],
backgroundColor: Colors.transparent,
elevation: 0,
),)
],
),
drawer: Theme(child: NavigatorDrawer(),
data: Theme.of(context).
copyWith(canvasColor: Colors.blueGrey.withOpacity(0.2)),
)
);
}
}