简体   繁体   English

BlocProvider.of() 调用的上下文不包含 MyBloc 类型的 Bloc/Cubit

[英]BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc

I tried to add MyBloc using BlocProvider, but I encountered context issue.我尝试使用 BlocProvider 添加 MyBloc,但遇到了上下文问题。 Error is as follows:错误如下:

Launching lib\main.dart on sdk gphone x86 arm in debug mode... lib\main.dart:1 √ Built build\app\outputs\flutter-apk\app-debug.apk. Launching lib\main.dart on sdk gphone x86 arm in debug mode... lib\main.dart:1 √ Built build\app\outputs\flutter-apk\app-debug.apk. Connecting to VM Service at ws://127.0.0.1:64346/zfCDazZFoQI=/ws在 ws://127.0.0.1:64346/zfCDazZFoQI=/ws 连接到 VM 服务

════════ Exception caught by gesture ═══════════════════════════════════════════
The following assertion was thrown while handling a gesture:
        BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc.

        No ancestor could be found starting from the context that was passed to BlocProvider.of<MyBloc>().

        This can happen if the context you used comes from a widget above the BlocProvider.

        The context used was: BlocAddPage(state: _BlocAddPageState#43fc1)

When the exception was thrown, this was the stack
#0      BlocProvider.of
package:flutter_bloc/src/bloc_provider.dart:121
#1      _BlocAddPageState.build.<anonymous closure>
package:example/…/examples/bloc_add.dart:53
#2      _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:993
#3      _InkResponseState.build.<anonymous closure>
package:flutter/…/material/ink_well.dart:1111
#4      GestureRecognizer.invokeCallback

I have following UI class.我有以下 UI class。

class BlocAddPage extends StatefulWidget {
      BlocAddPage({Key key}) : super(key: key);
      static const routeName = '/addBloc';
    
      @override
      _BlocAddPageState createState() => _BlocAddPageState();
    }
    
    class _BlocAddPageState extends State<BlocAddPage> {
      final Repository repository = Repository();
     
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              'Add',
            ),
          ),
          body: BlocProvider<MyBloc>(
            create: (context) => MyBloc(
                repository: context.read<Repository>(),
    
            child: ElevatedButton(
                onPressed: () {
                  BlocProvider.of<MyBloc>(context)
                      .add(AddEvent());
                },
                child: const Text('Submit'),
                style: ElevatedButton.styleFrom(
                  padding: EdgeInsets.all(15),
                  shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(40),
                    ),
                  ),
                )),

Problem is your context.问题是你的上下文。 wrap with a Builder .Builder包装。

Reason: When call BlocProvider.of<MyBloc>(context) , that of hopes your context is somewhere below the provider.原因:当调用BlocProvider.of<MyBloc>(context)of希望你的context位于提供者之下 But in your case, the context is the context of _BlocAddPageState which is the ancestor of the provider.但在您的情况下,上下文是_BlocAddPageState的上下文,它是提供者的祖先。 Thus I put a builder to solve it.于是我找了一个builder来解决它。

class BlocAddPage extends StatefulWidget {
      BlocAddPage({Key key}) : super(key: key);
      static const routeName = '/addBloc';
    
      @override
      _BlocAddPageState createState() => _BlocAddPageState();
    }
    
    class _BlocAddPageState extends State<BlocAddPage> {
      final Repository repository = Repository();
     
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              'Add',
            ),
          ),
          body: BlocProvider<MyBloc>(
            create: (context) => MyBloc(
                repository: context.read<Repository>(),
    
            child: Builder( // this!!!
              builder: (context) => ElevatedButton(
                onPressed: () {
                  BlocProvider.of<MyBloc>(context)
                      .add(AddEvent());
                },
                child: const Text('Submit'),
                style: ElevatedButton.styleFrom(
                  padding: EdgeInsets.all(15),
                  shape: const RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(
                      Radius.circular(40),
                    ),
                  ),
                ))),

暂无
暂无

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

相关问题 BlocProvider.of() 调用的上下文不包含 WeatherBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type WeatherBloc Flutter Bloc Cubit Navigator Problem BlocProvider.of() 调用的上下文不包含 CityCubit 类型的 Bloc/Cubit - Flutter Bloc Cubit Navigator Problem BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type CityCubit BlocProvider.of() 使用不包含 Cubit 类型的上下文调用 - BlocProvider.of() called with a context that does not contain a Cubit of type BlocProvider.of() 使用不包含 CounterBloc 类型的 Cubit 的上下文调用 - BlocProvider.of() called with a context that does not contain a Cubit of type CounterBloc Flutter 使用不包含 Bloc 类型的上下文调用的 BLoC BlocProvider.of() - Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type BlocProvider.of() 使用不包含 TrackingBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type TrackingBloc 使用不包含 FicheMvtBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type FicheMvtBloc BlocProvider.of() 使用不包含 CLASS 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type CLASS 使用不包含 Bloc 类型的上下文调用 Flutter BlocProvider.of() - Flutter BlocProvider.of() called with a context that does not contain a Bloc of type 使用不包含 TaharatBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type TaharatBloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM