lukepighetti / fluro

Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.
https://pub.dev/packages/fluro
MIT License
3.66k stars 416 forks source link

Expected a value of type 'Handler', but got one of type 'Null' #273

Open ghost opened 1 year ago

ghost commented 1 year ago

I have url : www.foobar.com/template/1234&token=1234 where the first 1234 is the template id. here is my code:

class CustomRouter {
  static FluroRouter router = FluroRouter();
  static final Handler _splashPage = Handler(
    handlerFunc: ((context, parameters) {
      return const SplashPage();
    }),
  );
  static final Handler _templatePageHandler = Handler(
    handlerFunc: ((context, parameters) {
      String templateId = parameters['tID']!.first;
      return const TemplatePage(templateId: templateId);
    }),
  );

  static void defineRoutes() {
    router.define("/", handler: _splashPage);
    router.define(
      "/template/:templateId",
      handler: _templatePageHandler,
      transitionType: TransitionType.fadeIn,
    );
  }
}

and the app.dart is as:

class App extends StatefulWidget {
  const App({Key? key}) : super(key: key);

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    super.initState();
    CustomRouter.defineRoutes();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Name",
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      onGenerateRoute: CustomRouter.router.generator,
    );
  }
}

Throws an Expected a value of type 'Handler', but got one of type 'Null'. When I tried running the same code but without the :templateId it works fine.

jdechicchis commented 1 year ago

I was hitting the same issue. Added the following as suggested here and it fixed it:

router.notFoundHandler = Handler(
        handlerFunc: (BuildContext? context, Map<String, dynamic> params) {
      return NotFoundPage();
    });