按下按钮时引发NoSuchMethod异常



你好,我正在学习flutter,我正在遵循一本书的教程来构建一个简单的应用程序。我正在构建的应用程序有亮模式和暗模式。当我点击在亮模式和暗模式之间切换的应用程序时,我遇到了一个例外。我的main.dart文件如下:

import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() => runApp(new AppWidget());
class AppWidget extends StatefulWidget {
AppWidget(){
debugPrint("AppWidget - constructor - " + hashCode.toString());
}
@override
_AppWidgetState createState() {
debugPrint("AppWidget - createState - " + hashCode.toString());
return _AppWidgetState();
}
}
class _AppWidgetState extends State<AppWidget>{
bool _bright = false;
_brightnessCallback(){
setState(() {
_bright = ! _bright;
});
}
@override
Widget build(BuildContext context) {
debugPrint("_AppWidgetState - build - " + hashCode.toString());
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
brightness: _bright ? Brightness.light : Brightness.dark,
),
home: FlowerWidget(
imageSrc : _bright ? "assets/images/light.jpg" : "assets/images/dark.jpg",
brightnessCallback: _brightnessCallback(),
),
);
}
}
class FlowerWidget extends StatefulWidget{
final String imageSrc;
final VoidCallback brightnessCallback;
FlowerWidget({Key key, this.imageSrc, this.brightnessCallback}) : super(key: key){
debugPrint("FlowerWidget - constructor - " + hashCode.toString());
}
@override
State<StatefulWidget> createState() {
debugPrint("FlowerWidget - createState - " +hashCode.toString()); return _FlowerWidgetState();
}
}
class _FlowerWidgetState extends State<FlowerWidget> {
double _blur=0;
_FlowerWidgetState(){
debugPrint("_FlowerWidgetState - constructor - " +hashCode.toString());
}
@override
initState() {
debugPrint("_FlowerWidgetState - initState - " +hashCode.toString()); 
}
@override
void didChangeDependencies() {
debugPrint(
"_FlowerWidgetState - didChangeDependencies - " +hashCode.toString()); 
}
@override
void didUpdateWidget(Widget oldWidget) {
debugPrint("_FlowerWidgetState - didUpdateWidget - " +hashCode.toString());
// The flower image has changed, so reset the blur.
_blur = 0;
}
void _blurMore(){
setState(() {
_blur += 0.5;
});
}
@override
Widget build(BuildContext context) {
debugPrint("_FlowerWidgetState - build - " +hashCode.toString());
return Scaffold(
appBar: new AppBar(
title: new Text("Flowers"),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.refresh),
onPressed: (){
widget.brightnessCallback();
},
)
],
),
body: new Container(
decoration: new BoxDecoration(
color: Theme.of(context).backgroundColor,
image: new DecorationImage(
image: AssetImage(widget.imageSrc),
fit: BoxFit.cover, 
),
),
child: new BackdropFilter(
filter: new ImageFilter.blur(sigmaX: _blur, sigmaY: _blur),
child: new Container(
decoration: new BoxDecoration(
color: Colors.white.withOpacity(0.0)
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _blurMore,
tooltip: 'Blur More',
child: Icon(Icons.add),
),
);
}
}

debugprint有许多功能,它们只是用于学习目的。我得到的例外看起来像:

════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'call' was called on null.
Receiver: null
Tried calling: call()

当我按下_FlowerWidgetState类的build方法中的刷新图标时,会引发异常。我正在努力消除这个例外。提前谢谢。

这是因为在构建FlowerWidget时,您传递的是回调函数值,而不是对回调的引用。

FlowerWidget( imageSrc : _bright ? "assets/images/light.jpg" : "assets/images/dark.jpg", brightnessCallback: _brightnessCallback(), )

所以,你所要做的就是传递函数名,而不使用像这样的括号

FlowerWidget( imageSrc : _bright ? "assets/images/light.jpg" : "assets/images/dark.jpg", brightnessCallback: _brightnessCallback, )

最新更新