Home > other >  Can I use BlocBuilder directly in BlocProvider in Flutter?
Can I use BlocBuilder directly in BlocProvider in Flutter?

Time:12-16

Can I use BlocBuilder directly in BlocProvider and have an access to a state in whole tree or should I use BlocBuilder on every widget separately if I plan to change it via state ?

Right now I have this construction on a top level and inject state to lower parts of the tree:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<NavigationCubit>(create: (context) => NavigationCubit())
      ],
      child: BlocBuilder<NavigationCubit, NavigationState>(
        builder: (context, state) {
          return WillPopScope(
            onWillPop: () async => false,
            child: const MaterialApp(
              home: RootContainer(state),
            ),
          );
        },
      ),
    );
  }
}

CodePudding user response:

well yes You can but its not recommended to do it in certain way

  • for small project and prototype ? sure
  • for large project ? big no

normally You want to separate things for as much as you can because like the name of it Blocbuilder it will rebuild widgets 'INSIDE' of it

so If you wraps with that many widgets example is like for post widget if you tap like Iconbutton, widget that need rebuild is just iconButton right but if you wrap a entire a post it will re render in User screen in 60 fps(standard)

so if you doing that way dnt do it again, because user experience will be awful when project goes large

tldr: its like setstate with extra steps if u doing that way

  • Related