【问题标题】:how can i solve blocProvider context problem?如何解决 blocProvider 上下文问题?
【发布时间】:2021-07-09 21:34:27
【问题描述】:

我有提交按钮,但是当我按下按钮时它给出了这个错误:

BlocProvider.of() 调用的上下文不包含 CreatePostCubit.
从古至今找不到祖先 传递给 BlocProvider.of() 的上下文。这 如果您使用的上下文来自 块提供者。 使用的上下文是:Builder

我想情况会很​​复杂。我该如何解决这个错误?

我的代码:

Widget createPostButton(BuildContext context) {
  final TextEditingController _postTitleController = TextEditingController();

  final TextEditingController _postDetailsController = TextEditingController();
  final TextEditingController _priceController = TextEditingController();

  final _formKey = GlobalKey<FormState>(debugLabel: '_formKey');
  return BlocProvider<CreatePostCubit>(
    create: (context) => CreatePostCubit(),
      child: Padding(
      padding: const EdgeInsets.only(right: 13.0, bottom: 13.0),
      child: FloatingActionButton(
        child: FaIcon(FontAwesomeIcons.plus),
        onPressed: () {
          showDialog(
              context: context,
              barrierDismissible: false,
              builder: (context) {
                return AlertDialog(
                  content: Form(
                    key: _formKey,
                    child: SingleChildScrollView(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          Padding(
                              padding: EdgeInsets.all(8.0),
                              child: TextFormField(
                                autocorrect: true,
                                controller: _postTitleController,
                                textCapitalization: TextCapitalization.words,
                                enableSuggestions: false,
                                validator: (value) {
                                  if (value.isEmpty || value.length <= 4) {
                                    return 'Please enter at least 4 characters';
                                  } else {
                                    return null;
                                  }
                                },
                                decoration:
                                    InputDecoration(labelText: 'Post Title'),
                              )),
                          Padding(
                              padding: EdgeInsets.all(8.0),
                              child: TextFormField(
                                controller: _postDetailsController,
                                autocorrect: true,
                                textCapitalization: TextCapitalization.words,
                                enableSuggestions: false,
                                validator: (value) {
                                  if (value.isEmpty || value.length <= 25) {
                                    return 'Please enter at least 25 characters';
                                  } else {
                                    return null;
                                  }
                                },
                                decoration: InputDecoration(
                                    labelText: 'Write a post details'),
                              )),
                          Padding(
                              padding: EdgeInsets.all(8.0),
                              child: TextFormField(
                                controller: _priceController,
                                enableSuggestions: false,
                                inputFormatters: <TextInputFormatter>[
                                  FilteringTextInputFormatter.digitsOnly
                                ],
                                keyboardType: TextInputType.number,
                                validator: (value) {
                                  if (value.isEmpty || value.length >= 4) {
                                    return 'Please enter a valid value';
                                  } else {
                                    return null;
                                  }
                                },
                                decoration:
                                    InputDecoration(labelText: 'Enter the Price'),
                              )),
                          OutlinedButton(
                            style: OutlinedButton.styleFrom(
                              primary: Colors.white,
                              backgroundColor: Colors.blue,
                            ),
                            child: Text("Submit"),
                            onPressed: () => {
                              BlocProvider.of<CreatePostCubit>(context)
                                  .createNewPost(
                                      postTitle: _postTitleController.text,
                                      postDetails: _postDetailsController.text,
                                      price: _priceController.text)
                            },
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              });
        },
      ),
    ),
  );
}

【问题讨论】:

    标签: flutter dart bloc


    【解决方案1】:

    您的showDialog 构建器正在使用新上下文。构建器返回的小部件不与最初调用 showDialog 的位置共享上下文。

    只需将 builder 参数重命名为其他名称

              showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (dialog_context) { // instead of context
                    return AlertDialog(
                    ....
    

    您还需要用构建器包装 BlocProvide 子级(以便它可以访问继承的 BlocProvider)

    BlocProvider<CreatePostCubit>(
        create: (context) => CreatePostCubit(),
        child: Builder(
            builder: (BuildContext context) => Padding(
            ...
    

    【讨论】:

    • BlocProvider.of() 使用不包含 CreatePostCubit 的上下文调用。它仍然给出这个
    • blocProvider 没有构建器属性。
    • 对不起,我正在使用不同的 blocProviders,正在更新 anwser
    【解决方案2】:

    在提交按钮中使用BlocProvider.of&lt;CreatePostCubit&gt;(context,listen:false) 而不是BlocProvider.of&lt;CreatePostCubit&gt;(context)

    【讨论】:

    • BlocProvider.of() 使用不包含 CreatePostCubit 的上下文调用。它仍然给出这个
    【解决方案3】:

    我认为问题在于 BlocProvider.of&lt;CreatePostCubit&gt;(context) 指的是没有 cubit 的原始上下文,因此请尝试将上下文重命名为 _context 之类的其他内容:

    create: (_context) => CreatePostCubit(),
    

    当这样打电话时

     BlocProvider.of<CreatePostCubit>(_context)
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多