Dart 调用'时出错;无效';在';onPressed/onTap';颤振
在onPressed/onTap中调用void时出错 我试图调用继承的小部件/状态中的设置状态 遵循此说明 在我不熟悉的人看来,他也在做同样的事, 但很明显,我错过了一些东西; 你能帮我吗Dart 调用'时出错;无效';在';onPressed/onTap';颤振,dart,flutter,void,Dart,Flutter,Void,在onPressed/onTap中调用void时出错 我试图调用继承的小部件/状态中的设置状态 遵循此说明 在我不熟悉的人看来,他也在做同样的事, 但很明显,我错过了一些东西; 你能帮我吗 class InhCore extends InheritedWidget { InhCore({Key key, @required Widget child, @required this.data}) : super(key: key, child: child); fi
class InhCore extends InheritedWidget {
InhCore({Key key, @required Widget child, @required this.data})
: super(key: key, child: child);
final InhState data;
@override
bool updateShouldNotify(InhCore oldWidget) {
return true;
}
}
class InhWidget extends StatefulWidget {
InhWidget({this.child});
final Widget child;
@override
State<StatefulWidget> createState() => InhState();
static InhState of(BuildContext context) {
return (context.inheritFromWidgetOfExactType(InhCore) as InhCore).data;
}
}
class InhState extends State<InhWidget> {
final Map<String, int> cardMap = {
'A':1,
'2':2,
'3':3,
'4':4,
'5':5,
'6':6,
'7':7,
'8':8,
'9':9,
'10':0,
'J':0,
'Q':0,
'K':0
};
List<String> cardDisplayed;
void deal(String card) => setState(() => cardDisplayed.add(card));
@override
Widget build(BuildContext context) {
return new InhCore(
data: this,
child: widget.child,
);
}
}
class CardButton extends StatelessWidget {
final String input;
CardButton({this.input});
Widget build(BuildContext context) {
final InhState state = InhWidget.of(context);
return Container(
width: 55.0,
height: 55.0,
child: Material(
color: Colors.grey[200],
child: InkWell(
onTap: state.deal(input),
child: Center (
child:Text(input),
),
),
),
);
}
}
内核中的类扩展了InheritedWidget{
InhCore({Key Key,@required Widget child,@required this.data})
:super(key:key,child:child);
最终状态数据;
@凌驾
bool updateShouldNotify(InhCore oldWidget){
返回true;
}
}
类InhWidget扩展了StatefulWidget{
InhWidget({this.child});
最后一个孩子;
@凌驾
State createState()=>InhState();
的静态状态(BuildContext上下文){
返回(context.inheritFromWidgetOfExactType(InhCore)作为InhCore);
}
}
类InhState扩展了State{
最终地图卡片地图={
"A":1,,
'2':2,
'3':3,
'4':4,
'5':5,
'6':6,
'7':7,
'8':8,
'9':9,
'10':0,
“J”:0,
“Q”:0,
“K”:0
};
显示列表卡;
作废交易(字符串卡)=>setState(()=>cardDisplayed.add(卡片));
@凌驾
小部件构建(构建上下文){
返回新的InhCore(
资料:这,,
child:widget.child,
);
}
}
类CardButton扩展了无状态小部件{
最终字符串输入;
CardButton({this.input});
小部件构建(构建上下文){
final InhState state=InhWidget.of(上下文);
返回容器(
宽度:55.0,
身高:55.0,
儿童:材料(
颜色:颜色。灰色[200],
孩子:InkWell(
onTap:state.deal(输入),
儿童:中心(
子:文本(输入),
),
),
),
);
}
}
提前感谢您的帮助您错过了()=>
onPressed: () => state.deal(input)
您的代码将调用的结果
state.deal(input)
传递到onPressed
,而上面的代码传递一个函数,当调用该函数时调用state.deal(input)谢谢,伙计,它起作用了我对继承的小部件显然还有其他问题(我觉得非常酷),但有一个步骤:)这为我解决了它,但非常奇怪的是,我在其他地方有相同的设置(复制和粘贴),只有在我忽略()=>的情况下才有效。显然,我不理解其中的区别。我需要看一个具体的例子。对于事件处理程序,您总是需要()=>
(单语句缩写形式)或(){…}
(块形式),除非调用“处理程序”返回一个成为实际事件处理程序的函数。