TAHA HORANE
TAHA HORANE

Reputation: 1

Could not find the correct Provider<CubitSubject> above this BlocBuilder widget in Flutter

I'm facing an issue with accessing my CubitSubject provider in a specific screen. I have a MultiBlocProvider in the root of my app, and I'm trying to access the CubitSubject in the SubjectsView screen. However, I get the following error message:

ProviderNotFoundException: Could not find the correct Provider<CubitSubject> above this BlocConsumer<CubitSubject, QuesAppStatus> Widget. This happens because you used a BuildContext that does not include the provider of your choice.


void main() async {
 
  WidgetsFlutterBinding.ensureInitialized();
  await Supabase.initialize(
    url: 'https://****.supabase.co', // URL الخاص بكs
    anonKey:
        '*******************', // مفتاح الوصول العام (مفتاح API)
  );
  runApp(MultiBlocProvider(
    providers: [
      BlocProvider(
        create: (context) => CubitTeacher(),
      ),
      BlocProvider(
        create: (context) => CubitSubject(),
      ),
    ],
    child: MaterialApp(
      routes: {
        LogInView.id: (context) => const LogInView(),
        HomeView.id: (context) => const HomeView(),
        SubjectsView.id: (context) => const SubjectsView(),
        TeacherSubjects.id: (context) => const TeacherSubjects(),
        GeneratedQuestionsView.id: (context) => const GeneratedQuestionsView(),
        TeacherProfileView.id: (context) => const TeacherProfileView(),
        ChangePasswordView.id: (context) => const ChangePasswordView(),
        CreateQuestionsView.id: (context) => const CreateQuestionsView(),
        CreateSubjectQuestionsView.id: (context) =>
            const CreateSubjectQuestionsView(),
        QuestionGenerateView.id: (context) => const QuestionGenerateView(),
        AddQuestion.id: (context) => const AddQuestion(),
      },
      theme:
          ThemeData(scaffoldBackgroundColor: kBackGround, fontFamily: 'Exo2'),
      debugShowCheckedModeBanner: false,
      initialRoute: LogInView.id,
    ),
  ));
}

}
`



class SubjectsView extends StatelessWidget {
  const SubjectsView({super.key});
  static String id = 'SubjectsView';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: customAppBar('المواد', context),
      body: Padding(
        padding: const EdgeInsets.all(18),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            const Align(
              alignment: Alignment.center,
              child: TeacherProfileCard(),
            ),
            const SizedBox(
              height: 20,
            ),
            BlocBuilder<CubitSubject, QuesAppStatus>(
              builder: (context, state) {
                if (state is SuccessState) {
                  return Expanded(
                      child: ListView.builder(
                    itemCount: state.subjects!.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.symmetric(vertical: 10),
                        child: CardSubjects(
                          onTap: () {},
                          subject: state.subjects![index].nameSubject,
                          teacherImag: 'assets/images/subjects.png',
                          classTeacher: state.subjects![index].classSabject,
                        ),
                      );
                    },
                  ));
                } else {
                  return const Text(
                    'لا يوجد اسئلة',
                    style: FontStyleApp.textStyleOrangeBold20,
                  );
                }
              },
            )
          ],
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 26

Answers (1)

Hassan Ismat Hassan
Hassan Ismat Hassan

Reputation: 41

try Move the multi bloc provider to be the parent of MaterialApp, also make sure that You are accessing the cubit with valid state, if not work try wrap your desired widget with the bloc provider first to make sure it works then go for further debugging to figure why it is not work since all setup had been done correctly

Upvotes: 0

Related Questions