Home > Software design >  "A build function returned null" after moving the exact same code into an other file and u
"A build function returned null" after moving the exact same code into an other file and u

Time:06-27

I want to tidy up my code, so I try to store the code of the user TopNavigationBar from TopNavigationBar in another file named "TopNavigationBarUser" and use it in TopNavigationBar. So I have less code in this file.

But when I use TopNavigationBarUser(); , then I get a build function returned null error.

error:

The following assertion was thrown building TopNavigationBarDesktop(dirty, dependencies: [_InheritedProviderScope<AuthProvider>]):
A build function returned null.

Does someone know why I get this error? I haven't changed anything in the code, I just moved it from TopNavigationBar into TopNavigationBarUser and call it in TopNavigationBar.

Before I moved the code it was actually working fine.

working fine:

TopNavigationBar.dart code:

if(currentRouteIsUser == true){
      return Container(
      height: 100,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[

          SizedBox(width: 30,),
          TopNavBarLogo(),
          SizedBox(width: 30,),
          Visibility(child: Text( "TheBestFitnessTracker", style: TextStyle(color: Colors.black, fontSize: 14, fontWeight: FontWeight.normal,))),

          Spacer(),      //Space between logo text and widgets in the center of the row
          TopNavBarItem('Informationen', InformationRoute),
          SizedBox(width: 30,),
          TopNavBarItem('Neuigkeiten', NeuigkeitenRoute),
          SizedBox(width: 30,),

          Spacer(), //Space between widgets in the center of the row and end of row
          SizedBox(width: 30,),
          TopNavBarItem('Login', AuthenticationPageRoute),
          SizedBox(width: 30,),
          TopNavBarItem('Teilehmer \n werden', RegristrationUserRoute),
          SizedBox(width: 30,),
        ],
      ),
    );
}

error:

TopNavigationBarUser.dart code:

import 'package:flutter/material.dart';
import '../../../routing/route_names.dart';
import '../../top_navbar_item/top_navbar_item.dart';
import '../top_navigation_bar_logo.dart';

class TopNavigationBarUser extends StatelessWidget {
  const TopNavigationBarUser({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[

          SizedBox(width: 30,),
          TopNavBarLogo(),
          SizedBox(width: 30,),
          Visibility(child: Text( "TheBestFitnessTracker", style: TextStyle(color: Colors.black, fontSize: 14, fontWeight: FontWeight.normal,))),

          Spacer(),      //Space between logo text and widgets in the center of the row
          TopNavBarItem('Informationen', InformationRoute),
          SizedBox(width: 30,),
          TopNavBarItem('Neuigkeiten', NeuigkeitenRoute),
          SizedBox(width: 30,),

          Spacer(), //Space between widgets in the center of the row and end of row
          SizedBox(width: 30,),
          TopNavBarItem('Login', AuthenticationPageRoute),
          SizedBox(width: 30,),
          TopNavBarItem('Teilehmer \n werden', RegristrationUserRoute),
          SizedBox(width: 30,),
        ],
      ),
    );
  }
}
 

using the TopNavigationbarUser in TopNavigationBar:

if(currentRouteIsUser == true){
      TopNavigationBarUser();
    }

Full TopNavigationBar code:

class TopNavigationBarDesktop extends StatelessWidget {
  const TopNavigationBarDesktop({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final authProvider = Provider.of<AuthProvider>(context);  // creating an instance of authProvider

    bool currentRouteIsAdmin = false;
    bool currentRouteIsUser = false;
    bool currentRouteIsScientist = false;
    final user = FirebaseAuth.instance.currentUser;

    // checking if an admin, an user or a scientist page is the currentRoute and then set the specific bool value to true
    Navigator.popUntil(context, (currentRoute) {
      if (currentRoute.settings.name == RegristrationScientistRoute || currentRoute.settings.name == RegristrationAdminRoute ||
          currentRoute.settings.name == DashboardRoute || currentRoute.settings.name == UsersAdministrationRoute) {
        currentRouteIsAdmin = true;
      }
      if (currentRoute.settings.name == RegristrationScientistRoute ) {
        currentRouteIsScientist = true;
      }
      if (currentRoute.settings.name == InformationRoute || currentRoute.settings.name == NeuigkeitenRoute ||
          currentRoute.settings.name == AuthenticationPageRoute || currentRoute.settings.name == RegristrationUserRoute ||
          currentRoute.settings.name == ForgotPasswordRoute || currentRoute.settings.name == AccessDeniedRoute) {
        currentRouteIsUser = true;
      }
      // Return true so popUntil() pops nothing.
      return true;
    });

    
    //specific bool value for user is true, then create the user TopNavigationBar
    if(currentRouteIsUser == true){
      TopNavigationBarUser();
    }
    }}

CodePudding user response:

It looks like you forgot to return TopNavigationBarUser. Add a return statement before TopNavigationBarUser:

if(currentRouteIsUser == true){
  return TopNavigationBarUser();
}
  • Related