简体   繁体   English

抛出 providernotfoundexception(t, context.widget.runtimetype);

[英]throw providernotfoundexception(t, context.widget.runtimetype);

I'm learning flutter and decided to work on a todo list application using cubit.我正在学习 flutter 并决定使用 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.我在主屏幕中使用 bloc provider 创建了一个肘,在另一个屏幕中我试图直接使用相同的肘而不创建另一个。

Homescreen cubit section and creating database using cubit:主屏幕 cubit 部分和使用 cubit 创建数据库:

I created the cubit here and created the database.我在这里创建了 cubit 并创建了数据库。

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.在第二页上,我试图在不使用 bloc 提供程序的情况下使用 cubit。 When I use bloc provider somehow I cant access the data in the database and I have to call create database again.当我以某种方式使用 bloc provider 时,我无法访问数据库中的数据,我必须再次调用创建数据库。

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;
  }

Have you tried using the BlocProvider.value() Widget?您是否尝试过使用BlocProvider.value()小部件?

For example:例如:

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

I fixed the issue by creating the cubit before material app我通过在材料应用程序之前创建 cubit 解决了这个问题

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);
       }));
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期的 ';' 在这之后。 , stthrow ProviderNotFoundException(T, context.widget.runtimeType) - Expected ';' after this. , stthrow ProviderNotFoundException(T, context.widget.runtimeType) flutter 中的 ${widget.runtimeType} 没有 TabController - No TabController for ${widget.runtimeType} in flutter Flutter:无法调用提供商<t> .of(context) 来自定义在另一个文件中的 function。 ProviderNotFoundException</t> - Flutter: Can't call Provider<T>.of(context) from a function that is defined into another file. ProviderNotFoundException ProviderNotFoundException(错误:找不到正确的提供者<entryprovider>在此主页小部件上方</entryprovider> - ProviderNotFoundException (Error: Could not find the correct Provider<EntryProvider> above this HomePage Widget ProviderNotFoundException(错误:找不到正确的提供者<GroupModel>在此 GroupScreen 小部件上方 - ProviderNotFoundException (Error: Could not find the correct Provider<GroupModel> above this GroupScreen Widget 返回类型“小部件?” 不是闭包上下文所要求的“小部件” - The return type 'Widget?' isn't a 'Widget', as required by the closure's context ProviderNotFoundException(错误:找不到正确的提供者<layoutdata>在此 SchedulingPage Widget 上方使用 layoutBuilder</layoutdata> - ProviderNotFoundException (Error: Could not find the correct Provider<LayoutData> above this SchedulingPage Widget using layoutBuilder ProviderNotFoundException 被抛出 - ProviderNotFoundException was thrown 根据闭包上下文的要求,返回类型“Person”不是“Widget” - The return type 'Person' isn't a 'Widget', as required by the closure's context 返回类型不是闭包上下文所要求的小部件 - The return type isn't a widget as required by the closures context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM