1

I have to pass parameter to Future async function from floatingbutton

My Codes like

FloatingActionButton(
                     onPressed: getImageFromCam,
                      tooltip: 'Pick Image',
                      child: Icon(Icons.add_a_photo),
                     ),

And

     Future getImageFromCam() async { // for camera
        var image = await ImagePicker.pickImage(source: ImageSource.camera);
        setState(() {
          _image = image;
        });
}

It is multiple button so i have to pass index to async function.

Can anyone please help to solve this.

Thanks in advance

Sathish

1 Answer 1

4

You cannot explicitly specify any argument when using a tear-off. Instead, you should manually define a closure:

FloatingActionButton(
  onPressed: () => getImageFromCam(index),
  tooltip: 'Pick Image',
  child: Icon(Icons.add_a_photo),
);

...

Future<void> getImageFromCam(int index) async {
  // Do whatever you want with `index`.
  final image = await ImagePicker.pickImage(source: ImageSource.camera);
  setState(() => _image = image);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.