Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 如何在颤振中向其他类注入函数_Function_Flutter_Methods_Dart_Inject - Fatal编程技术网

Function 如何在颤振中向其他类注入函数

Function 如何在颤振中向其他类注入函数,function,flutter,methods,dart,inject,Function,Flutter,Methods,Dart,Inject,我想在单击我的按钮时打印“测试” 我在我的类中用一个函数构造了一些结构,但不起作用 请帮帮我 这是我的密码 Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'bla bla bla', ), MyButton((){print('test

我想在单击我的按钮时打印“测试”

我在我的类中用一个函数构造了一些结构,但不起作用

请帮帮我 这是我的密码

Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'bla bla bla',
            ),
            MyButton((){print('test');}, 'my button')
          ],
        ),
列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
"呜呜呜呜",,
),
MyButton((){print('test');},'MyButton')
],
),
我创建了一个StatefulWidget类,我称之为MyButton

import 'package:flutter/material.dart';

class MyButton extends StatefulWidget {
  Function buttonFunction;
  String buttonName;
  MyButton(this.buttonFunction,this.buttonName);

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

class _MyButtonState extends State<MyButton> {
  @override
  Widget build(BuildContext context) {
    String name=widget.buttonName;
    return Container(
      width: 200,
      child: RaisedButton(
        color: Colors.red,
        onPressed: () {
          widget.buttonFunction;
          print('clicked $name');
        },

        textColor: Colors.white,
        child: Text("$name",
          style: TextStyle(fontSize: 18,),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
类MyButton扩展StatefulWidget{
功能按钮功能;
字符串按钮名;
MyButton(this.buttonFunction,this.buttonName);
@凌驾
_MyButtonState createState()=>\u MyButtonState();
}
类_MyButtonState扩展状态{
@凌驾
小部件构建(构建上下文){
字符串名称=widget.buttonName;
返回容器(
宽度:200,
孩子:升起按钮(
颜色:颜色,红色,
已按下:(){
widget.buttonFunction;
打印('clicked$name');
},
textColor:Colors.white,
子项:文本(“$name”,
样式:TextStyle(字体大小:18,),
),
),
);
}
}

调用函数时缺少括号“()”,而只是引用它

您还在build方法中设置了一个变量,该变量是声明变量的错误位置,因为它将多次重新运行,并且会被不必要地重新声明,并且代价高昂。如果要访问字符串中某个值的属性,只需使用`“String${widget.myStringVariable}”

我修改了您的代码以反映这些更改:

class _MyButtonState extends State<MyButton> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 200,
      child: RaisedButton(
        color: Colors.red,
        onPressed: () {
          widget.buttonFunction();
          print('clicked $name');
        },
        textColor: Colors.white,
        child: Text("${widget.buttonName}",
          style: TextStyle(fontSize: 18,),
        ),
      ),
    );
  }
}
class\u MyButtonState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
宽度:200,
孩子:升起按钮(
颜色:颜色,红色,
已按下:(){
widget.buttonFunction();
打印('clicked$name');
},
textColor:Colors.white,
子项:文本(${widget.buttonName}),
样式:TextStyle(字体大小:18,),
),
),
);
}
}

您没有调用函数:
widget.buttonFunction()
@Husamuldeen是一个解决您问题的答案,请记住将其标记为正确。它将帮助其他用户。下面的投票箭头是一个复选标记。如果您按下它,它将变为绿色,并将答案标记为正确。如果我想在重写之前在If语句中使用widget.buttonName,我该怎么办?请帮助@João Soares您可以像任何其他变量一样使用它
String name=widget.buttonName