skip to Main Content

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?

2

Answers


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

    // widget.variableName
    widget.callback(true);
    
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search