Home > database >  Is it okay declare functions inside the build method?
Is it okay declare functions inside the build method?

Time:11-23

I have been declaring functions inside the build method and now I am wondering whether this is a bad practice.

@override
Widget build(BuildContext context) {

  void doRegister() async {
    ...
  }

  return Scaffold(
    ...
  );
}

Are there any problems with this approach and should I declare functions outside of the build method?

CodePudding user response:

As far as I'm aware, it's fine. Just be careful not to make function calls inside your build method (like fetching from an API to fill a listview with data), as it will flood your API and make your app perform quite terribly.

CodePudding user response:

No, You should not declare a function in the build method, the build method may call several times and you shouldn't have logic in your build method. declare method outside of build method or use state management like bloc.

CodePudding user response:

It's bad practice because the method will call unnecessarily every time you build the widget. It will make your app performance slow .

  • Related