“Dart callback isn’t defined for the type _SignaturePadState.”

huangapple go评论60阅读模式
英文:

Dart callback isn't defined for the type _SignaturePadState

问题

I'm trying to use callback for dart to retrieve value from a child in dart if the user already input their signature. This is what i have but the callback function is highlighted in red when I try to use it stating:

我正在尝试在Dart中使用回调来从子组件中检索值,以确定用户是否已输入他们的签名。这是我的代码,但当我尝试使用回调函数时,它显示为红色,并显示以下错误信息:

The method 'callback' isn't defined for the type '_SignaturePadState'.

方法'callback'未定义于类型'_SignaturePadState'。

Callback returns a boolean value based on the api response. This is what I have.

回调函数根据API响应返回一个布尔值。这是我的代码。

PARENT(父组件)

bool hasSigned = false;

bool _updateValueSigned(bool value) {
  setState(() {
    hasSigned = value;
  });
  return value;
}

CHILD(子组件)

class SignaturePad extends StatefulWidget {
  final void Function(bool) callback;

  SignaturePad({required this.callback});

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

@override
Widget build(BuildContext context) {
    //.... other codes
body: Container(
      padding: const EdgeInsets.all(18),
//... lines of code

ElevatedButton(
  onPressed: () async {
    callback(true); // this line throws the error mentioned above.
  },
  child: Text('Save'.toUpperCase()),
  style: ElevatedButton.styleFrom(
    backgroundColor: Theme.of(context).primaryColor,
  ),
),

Can anyone help me figure this out?

谁能帮我解决这个问题?

英文:

I'm trying to use callback for dart to retrieve value from a child in dart if the user already input their signature. This is what i have but the callback function is highlighted in red when I try to use it stating:

The method 'callback' isn't defined for the type '_SignaturePadState'.

Callback returns a boolean value based on the api response. This is what I have.

PARENT

bool hasSigned = false;

bool _updateValueSigned(bool value) {
  setState(() {
    hasSigned = value;
  });
  return value;
}

CHILD

class SignaturePad extends StatefulWidget {
  final void Function(bool) callback;

  SignaturePad({required this.callback});

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

@override
Widget build(BuildContext context) {

    //.... other codes
body: Container(
      padding: const EdgeInsets.all(18),
//... lines of code

ElevatedButton(
  onPressed: () async {

    callback(true); // this line throws the error mentioned above.
  },
  child: Text('Save'.toUpperCase()),
  style: ElevatedButton.styleFrom(
    backgroundColor: Theme.of(context).primaryColor,
  ),
),

Can anyone help me figure this out?

答案1

得分: 1

你正在尝试从状态类中调用小部件变量,将会是

// widget.variableName
widget.callback(true);
英文:

You are trying to call widget variable from state class, It will be

// widget.variableName
widget.callback(true);

答案2

得分: 0

当你想要使用从状态类声明的小部件变量时,你需要像这样写://widget.variable,在你的情况下,它将是:

widget.callback(true);
英文:

Whenever You want to use widget variable declare from state class, you need to
write like this //widget.variable, In ur case it will be

widget.callback(true);

huangapple
  • 本文由 发表于 2023年4月17日 15:57:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定