0

I got a code which works but it doesn't use my routing system :

          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => ChatScreen(
                chatParams: ChatParams(currentUser.uid, userFriend!),
              ),
            ),
          );

I load perfectly my page. Problems : of course, I want to use my route system. So i do this :

          Navigator.pushNamed(
            context,
            RouteConsts.chatScreen,
            arguments: ChatParams(currentUser.uid, userFriend!),
          );

Name of route :

class RouteConsts {
  ...
  static const String chatScreen = '/ChatScreen';
  ...
}

And the route concerned :

class RouteGenerator {
  static Route<dynamic> generate(BuildContext context, RouteSettings settings) {
    Widget page = Center(child: Text('Route isn't accessible')); 

    switch (settings.name) {
      case RouteConsts.chatScreen:
        print("on y est");
        if (settings.arguments is ChatParams) {
          ChatParams chatParams = settings.arguments as ChatParams;
          print(chatParams);
          return MaterialPageRoute(
            builder: (context) => ChatScreen(chatParams: chatParams),
          );
        } else {
          print("Unexpected arguments type for ChatScreen: ${settings.arguments.runtimeType}");
          return MaterialPageRoute(builder: (_) => ItemsListScreen());
          // Vous pouvez également retourner null ou une autre route de secours si nécessaire
        }
        break;
      default:
        page = Center(child: Text('La route n\'est pas accessible'));
        break;
    }

The problem seems to come from the break, which is considered like a "deadcode" by IDE. This is because i use a return of MaterialPageRoute, so my switch doesn't like it. Any idea to resolve this ?

Thanks for your time.

0

Browse other questions tagged or ask your own question.