Home > Net >  passing function in dart flutter returing null
passing function in dart flutter returing null

Time:01-06

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.

CodePudding user response:

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;
  •  Tags:  
  • Related