0

Hi i dont know what i did wrong here i converted a button to a class on it own and it stopped working it keep returning null and i dont know why. I have adjusted the code and the button class does not give any errors. Take a look at the code below

HERE IS MY BUTTON CLASS

class BottomButtonIcon extends StatelessWidget {
  const BottomButtonIcon({required this.buttonText, required this.ontap});
  final String buttonText;
  final Void Function() ontap;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: ontap,
        child: Container(
          child: Center(
            child: Text(buttonText),
          ),
          padding: const EdgeInsets.only(bottom: 20),
          margin: kbuttomContainerMargin,
          color: kbuttomContainerColor,
          height: kbuttomContainerHeight,
          width: double.infinity,
        ));
  }
}

CALL BUTTON CLASS AND PASSING SETSTATE AND THEN NAVIGATOR.PUSH

  BottomButtonIcon(
              buttonText: 'CALCULATE',
              ontap: () {
                setState(() {
                  Navigator.push(context,
                      MaterialPageRoute(builder: (context) => const ResultPage()));
                });
              },
            ),

ERROR MESSAGE IS: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. Try adding either a return or a throw statement at the end.

1
  • When i correct a couple of lines in your code, I don't get any error... i corrected this line final void Function() ontap; and deleted these linesmargin: kbuttomContainerMargin, color: kbuttomContainerColor, height: kbuttomContainerHeight, Commented Jan 5, 2022 at 19:59

1 Answer 1

1

It seems like you misspelled your function declaration:

final Void Function() ontap;

I don't know what Void is but I believe it has something to do with c++ support, please don't quote me on that tho. It should actually be void with no capital letters:

final void Function() onTap;

sidenote, on flutter there is a type called VoidCallback that is an equivalent of void Function(), most flutter callbacks use it and you can use it aswell if you want:

final VoidCallback onTap;
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.