Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter build function returns null

I am trying to navigate to my home page or login page on the basis of authStatus. While using Navigator.of(context)... it returns null on the build method for a second and then screen refreshes and navigate to the given page successfully. I am relatively new to mobile development and flutter. Any leads will help! Thanks.

Here is the code:

  @override
  Widget build(BuildContext context) {
    switch (authStatus) {
      case AuthStatus.NOT_LOGGED_IN:
        WidgetsBinding.instance.addPostFrameCallback((_) {
          {
            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => LoginSignUpPage(
                        auth: widget.auth,
                        onSignedIn: _onLoggedIn,
                        params: widget.params,
                      )),
            );
          }
        });

        break;
      case AuthStatus.LOGGED_IN:
        if (_userId.length > 0 && _userId != null) {
          WidgetsBinding.instance.addPostFrameCallback((_) {
            Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => HomePage(
                        userId: _userId,
                        auth: widget.auth,
                        onSignedOut: _onSignedOut,
                        params: widget.params,
                      )),
            );
          });
        } else
          return widget.waitingScreen;
        break;
      default:
        return widget.waitingScreen;
    }

Here is the error :

A build function returned null. The offending widget is: RootPage Build functions must never return null. To return an empty space that causes the building widget to fill available room, return "Container()". To return an empty space that takes as little room as possible, return "Container(width: 0.0, height: 0.0)".

like image 961
Maaz Ahmed Avatar asked Feb 28 '26 06:02

Maaz Ahmed


1 Answers

on the code line following WidgetsBinding, add

return Container();

You could also include a color parameter to match your design.

like image 63
JonnyH Avatar answered Mar 03 '26 01:03

JonnyH