Home > other >  How to fix Navigator not working in flutter bloc pattern?
How to fix Navigator not working in flutter bloc pattern?

Time:05-02

I am trying to make an application using Pokemon api as an exercise. I am trying to make it using the Bloc pattern. If you click on GridTile, you need to get indexID and move to DetailPage. Getting the IndexID is fine, but it doesn't go to the page. I can't seem to build the page, but I don't know what the problem is. It would be really appreciated if you could tell me what the problem is.

I put the code on github because it would be good to see the whole code.

https://github.com/Jun-Kor/pokedex.git

CodePudding user response:

The main problem is not your navigation, it's your Bloc, what happens is that when you enter the PokemonDetailsView page, with the BlocBuilder<PokemonDetailsBloc, PokemonDetailsState> it cannot find its provider (the one in the main) but, if we go directly there, you have it written, right ?

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: MultiBlocProvider( // here is the problem
        providers: [
          BlocProvider<PokemonBloc>(
              create: (context) =>
                  PokemonBloc()..add(const PokemonPageRequestEvent(page: 0))),
          BlocProvider<PokemonDetailsBloc>(
              create: (context) => PokemonDetailsBloc()),
        ],
        child: const PokedexView(),
      ),
    );
  }
}

Well, the MultiBlocProvider has to be the main parent that is going to provide for the whole MaterialApp, not be a child of it, I mean ? That's why the problem was asking for a Bloc provider, but I it couldn't find one.

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(  // change here
      providers: [
        BlocProvider<PokemonBloc>(
            create: (context) =>
                PokemonBloc()..add(const PokemonPageRequestEvent(page: 0))),
        BlocProvider<PokemonDetailsBloc>(
            create: (context) => PokemonDetailsBloc()),
      ],
      child: MaterialApp(
        title: 'Material App',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.red,
        ),
        home: const PokedexView(),
      ),
    );
  }
}
  • Related