Search code examples
flutterflutter-dependenciesflutter-go-router

go-router returning 'Null' is not subtype of String


I'm trying to go-router on my routing. First time it works but once I refresh it works.


final GoRouter _router = GoRouter(
  debugLogDiagnostics: true,
  initialLocation: '/',
  routes: [
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return const MainPage();
      },
    ),
    GoRoute(
      path: '/product/:id',
      name: 'product',
      builder: (context, state){
        final id = state.params['id']!;
        return DetailsView(id:id);
        },
    ),
    GoRoute(
      path: '/homepage',
      builder: (BuildContext context, GoRouterState state) {
        return const HomePage();
      },
    ),
  ],
);

Even when passing any value it doesn't work

onTap: () {
        context.pushNamed("product", params: {'id':"dsadklaskld"});
   
      },

Here is the stateful widget which I'm navigating to. detailsView.dart


class DetailsView extends StatefulWidget {
  final String id;

  /// Constructs a [DetailsScreen]
  const DetailsView({super.key, required this.id});
  @override
  State<DetailsView> createState() => _DetailsViewState();
}
class _DetailsViewState extends State<DetailsView> {
.... 
  late String id = widget.id;
}

Solution

  • The value of id was null so I had to put it in initializedata() function.