Home > Back-end >  throw providernotfoundexception(t, context.widget.runtimetype);
throw providernotfoundexception(t, context.widget.runtimetype);

Time:10-08

I'm learning flutter and decided to work on a todo list application using cubit. I am created a cubit using bloc provider in the homescreen and in another screen I'm trying to consume the same cubit directly without creating another one.

Homescreen cubit section and creating database using cubit:

I created the cubit here and created the database.

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

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => appcubit()..CreateDatabase(),
      child: BlocConsumer<appcubit, appStates>(
        listener: (context, state) {
          // ignore: todo
          // TODO: implement listener
        },
        builder: (context, state) {
          appcubit cubit = appcubit.get(context);

          return Scaffold(

I have a button that directs to a second page:

Widget buildTaskCat(tasknum, cat, progress, context) {
  return InkWell(
    onTap: () {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => cattaskview(
            cat: cat,
            progress: progress,
            tasknum: tasknum,
          ),
        ),
      );
    },

On the second page Im trying to consume the cubit without using bloc provider. When I use bloc provider somehow I cant access the data in the database and I have to call create database again.

class cattaskview extends StatelessWidget {
  const cattaskview(
      {Key? key,
      required this.cat,
      required this.tasknum,
      required this.progress})
      : super(key: key);
  final String cat;
  final int tasknum;
  final double progress;

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<appcubit, appStates>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        return Scaffold(

I get this error message when I try to run

if (inheritedElement == null && null is! T) {
      throw ProviderNotFoundException(T, context.widget.runtimeType);
    }

    return inheritedElement;
  }

CodePudding user response:

Have you tried using the BlocProvider.value() Widget?

For example:

Navigator.push(
  context,
  MaterialPageRoute(
    builder: (context) => BlocProvider.value(
      value: BlocProvider.of<appcubit>(context)
      child: cattaskview(),
    )
  )
);

CodePudding user response:

I fixed the issue by creating the cubit before material app

void main() {
 Bloc.observer = MyBlocObserver();
 runApp(const Todo());
}

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

 @override
 Widget build(BuildContext context) {
   return BlocProvider(
       create: (context) => appcubit()..CreateDatabase(),
       child: BlocConsumer<appcubit, appStates>(listener: (context, state) {
         // TODO: implement listener
       }, builder: (context, state) {
         return MaterialApp(
             theme: ThemeData(
               appBarTheme: const AppBarTheme(
                 systemOverlayStyle: SystemUiOverlayStyle(
                   statusBarColor: Colors.transparent,
                   statusBarIconBrightness: Brightness.light,
                 ),
               ),
             ),
             home: Homescreen(),
             debugShowCheckedModeBanner: false);
       }));
 }
}

  • Related