Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion Failure in go_router package: 'package:go_router/src/parser.dart': Failed assertion: line 63 pos 12: 'routeInformation.state != null'

The error is within the go_router package and is related to an assertion failure in the parser.dart file. The specific assertion that failed is at line 63, position 12, and it checks whether routeInformation.state is not null. The assertion is there to ensure that the state associated with a route is not null.

'package:go_router/src/parser.dart': Failed assertion: line 63 pos 12: 'routeInformation.state != null': is not true.

my AppRouts class

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:quizapp/screens/home/home.dart';
import '../../screens/error/error_page.dart';
import '../../screens/splash/splash_screen.dart';
import 'app_routers.dart';

class MyAppRouts {
  GoRouter route = GoRouter(
    routes: [
      GoRoute(
        name: MyAppRoutesConstent.splashScreenRoutename,
        path: '/',
        pageBuilder: (context, state) {
          return const MaterialPage(child: SplashScreen());
        },
      ),
      GoRoute(
        name: MyAppRoutesConstent.homeRoutename,
        path: '/home',
        pageBuilder: (context, state) {
          return const MaterialPage(child: HomePage());
        },
      ),
    ],
    errorPageBuilder: (context, state) {
      return const MaterialPage(child: ErrorPage());
    },
  );
}

Im new to GoRouter package what is the problem hear i need lord splash screen as my first screen and this is my main.dart file

import 'package:flutter/material.dart';

import 'project/routes/app_routers_const.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Quiz App',
      debugShowCheckedModeBanner: false,
      routeInformationParser: MyAppRouts().route.routeInformationParser,
      routerDelegate: MyAppRouts().route.routerDelegate,
    );
  }
}

i need lord splash screen as my first screen

like image 775
Ashen madalagama Avatar asked Sep 03 '25 17:09

Ashen madalagama


2 Answers

As per the documentation, configure the MaterialApp.router like this:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: MyAppRouts().route,
    );
  }
}

and set an initial path for splash screen:

GoRouter route = GoRouter(initialLocation : "/splashScreen" , ..);
like image 136
Vivek Chib Avatar answered Sep 05 '25 16:09

Vivek Chib


if you are still wondering what's wrong just make sure you do this,

AppRouts class

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:quizapp/screens/home/home.dart';
import '../../screens/error/error_page.dart';
import '../../screens/splash/splash_screen.dart';
import 'app_routers.dart';

class MyAppRouts {
  GoRouter route = GoRouter(
    initialLocation: "/", // Any location you want to show first
    routes: [
      GoRoute(
        name: MyAppRoutesConstent.splashScreenRoutename,
        path: '/',
        pageBuilder: (context, state) {
          return const MaterialPage(child: SplashScreen());
        },
      ),
      GoRoute(
        name: MyAppRoutesConstent.homeRoutename,
        path: '/home',
        pageBuilder: (context, state) {
          return const MaterialPage(child: HomePage());
        },
      ),
    ],
    errorPageBuilder: (context, state) {
      return const MaterialPage(child: ErrorPage());
    },
  );
}

main.dart

import 'package:flutter/material.dart';

import 'project/routes/app_routers_const.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      title: 'Flutter Quiz App',
      debugShowCheckedModeBanner: false,
      routerConfig: MyAppRouts().route,
      //make sure you must not add any other data like 
      //"routeInformationParser" and "routerDelegate" they'll cause error
    );
  }
}
like image 24
Tanmay Bhatgare Avatar answered Sep 05 '25 14:09

Tanmay Bhatgare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!