0

I have a flutter project, and i am using auto_route: ^7.8.3 package for navigation i have set up a bottom nav bar with 4 tabs and a screen for each tab when i run it it works as i expect it, my issue is i am trying to set up some widget test and get a error this is my code

 void main() {
  runApp(App());
}

class App extends StatelessWidget {
  final _appRouter = AppRouter();

  App({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _appRouter.config(),
    );
  }
}

and this is the screen that displays everything when the app first runs


@RoutePage()
class InitialScreen extends StatelessWidget {
  const InitialScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return AutoTabsRouter.tabBar(
      routes: const [
        HomeRoute(),
        MyWorkoutsRoute(),
        ChatRoute(),
        SettingsRoute()
      ],
      builder: (context, child, controller) {
        final tabsRouter = AutoTabsRouter.of(context);

        return Scaffold(
          appBar: AppBar(
            systemOverlayStyle: SystemUiOverlayStyle.dark,
            backgroundColor: Colors.teal,
            title: Text(
              context.topRoute.name,
              style: const TextStyle(
                fontSize: 25,
                fontWeight: FontWeight.bold,
                fontStyle: FontStyle.italic,
                color: Colors.black87,
              ),
            ),
            leading: AutoLeadingButton(),
          ),
          body: child,
          bottomNavigationBar: _buildBottomNavigationBar(tabsRouter),
        );
      },
    );
  }

  AppBar _buildAppBar(BuildContext context, TabsRouter tabsRouter) {
    return AppBar(
      systemOverlayStyle: SystemUiOverlayStyle.dark,
      backgroundColor: Colors.teal,
      title: Text(
        context.topRoute.name,
        style: const TextStyle(
          fontSize: 25,
          fontWeight: FontWeight.bold,
          fontStyle: FontStyle.italic,
          color: Colors.black87,
        ),
      ),
      leading: const AutoLeadingButton(),
    );
  }

  Widget _buildBottomNavigationBar(TabsRouter tabsRouter) {
    return SafeArea(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: GNav(
          onTabChange: (index) {
            tabsRouter.setActiveIndex(index);
          },
          rippleColor: Colors.teal.shade800,
          hoverColor: Colors.teal.shade700,
          haptic: true,
          tabBorderRadius: 15,
          duration: const Duration(milliseconds: 700),
          gap: 8,
          color: Colors.teal,
          activeColor: Colors.teal,
          iconSize: 40,
          tabBackgroundColor: Colors.black87,
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
          tabs: [
            _buildNavButton(Icons.home, 'Home'),
            _buildNavButton(Icons.fitness_center_sharp, 'Likes'),
            _buildNavButton(Icons.mail_outline, 'Chat'),
            _buildNavButton(Icons.settings, 'Settings'),
          ],
        ),
      ),
    );
  }

  GButton _buildNavButton(IconData icon, String text) {
    return GButton(
      icon: icon,
      text: text,
      textStyle: const TextStyle(
        fontSize: 20,
        color: Colors.teal,
        fontWeight: FontWeight.bold,
      ),
    );
  }
}

this is my widget test i am just trying to set it up to start with

void main() {
  testWidgets('App initializes and renders without errors',
      (WidgetTester tester) async {
    await tester.pumpWidget(const MaterialApp(
      home: InitialScreen(),
    ));
  });
}

when i run it i get the following error

Exception has occurred.
FlutterError (RouteData operation requested with a context that does not include an RouteData.
The context used to retrieve the RouteData must be that of a widget that is a descendant of a AutoRoutePage.)

i saw on here that someone suggested that the following works

void main() {
  testWidgets('App initializes and renders without errors',
      (WidgetTester tester) async {
    await tester.pumpWidget(
      StackRouterScope(
        controller: MockAppRouter(),
        stateHash: 0,
        child: InitialScreen(),
      ),
    );
  });
}

but i get the same error any help appreciated i am very new to flutter so apologies if my explanation is not great

farooq
  • 9
  • 2

0 Answers0