简体   繁体   English

上下文不包含块

[英]Context does not contain bloc

in the following test code, I am trying to send an event to the TestBloc, when a button in the app bar is clicked.在下面的测试代码中,当单击应用栏中的按钮时,我试图向 TestBloc 发送一个事件。 I moved the BlocBuilder outwards around the scaffold.我将 BlocBuilder 围绕脚手架向外移动。 But when the _reloadData method is called, then the following error pops up但是当调用_reloadData方法时,则弹出如下错误

 "BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type TestBloc"

Code代码

class _TestPageState extends State<TestPage> with UiPresenter {
  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => sl<TestBloc>()..add(GetDataEvent()),
      child: Scaffold(
        appBar: AppBar(
            title: AppBarTitleWidget(
          title: 'Test',
          onRefreshPressed: () => _reloadData(context),
        )),
        drawer: MainMenuDrawer(),
        body: ContentContainer(
          header: Text(
            'Test',
            style: Theme.of(context).textTheme.headline1,
          ),
          child: _buildBody(context),
    ),
      ),
    );
  }


_reloadData(BuildContext context) {
    BlocProvider.of<TestBloc>(context).add(GetDataEvent());
}

You can copy paste run full code below您可以在下面复制粘贴运行完整代码
I use the following full code to simulate this case我使用下面的完整代码来模拟这种情况
You can wrap AppBarTitleWidget with Builder您可以使用Builder包装AppBarTitleWidget
code snippet代码片段

    appBar: AppBar(title: Builder(builder: (BuildContext context) {
          return AppBarTitleWidget(
            title: "Test",
            onRefreshPressed: () => _reloadData(context),
          );
        }))

working demo工作演示

在此处输入图像描述

full code完整代码

import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class TestBloc extends Cubit<int> {
  /// {@macro counter_cubit}
  TestBloc() : super(0);

  /// Add 1 to the current state.
  void GetDataEvent() => emit(state + 1);

  /// Subtract 1 from the current state.
  void decrement() => emit(state - 1);
}

/*class CounterPage extends StatelessWidget {
  /// {@macro counter_page}
  const CounterPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => CounterCubit(),
      child: CounterView(),
    );
  }
}*/

/// {@template counter_observer}
/// [BlocObserver] for the counter application which
/// observes all [Cubit] state changes.
/// {@endtemplate}
class CounterObserver extends BlocObserver {
  @override
  void onChange(Cubit cubit, Change change) {
    print('${cubit.runtimeType} $change');
    super.onChange(cubit, change);
  }
}

void main() {
  Bloc.observer = CounterObserver();
  runApp(CounterApp());
}

class CounterApp extends MaterialApp {
  /// {@macro counter_app}
  CounterApp({Key key}) : super(key: key, home: TestPage());
}

_reloadData(BuildContext context) {
  BlocProvider.of<TestBloc>(context).GetDataEvent();
}

class TestPage extends StatefulWidget {
  @override
  _TestPageState createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;
    return BlocProvider(
      create: (_) => TestBloc(),
      child: Scaffold(
        appBar: AppBar(title: Builder(builder: (BuildContext context) {
          return AppBarTitleWidget(
            title: "Test",
            onRefreshPressed: () => _reloadData(context),
          );
        })),
        body: Center(
          child: BlocBuilder<TestBloc, int>(
            builder: (context, state) {
              return Text('$state', style: textTheme.headline2);
            },
          ),
        ),
        floatingActionButton: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: <Widget>[
            FloatingActionButton(
              key: const Key('counterView_increment_floatingActionButton'),
              child: const Icon(Icons.add),
              onPressed: () => context.read<TestBloc>().GetDataEvent(),
            ),
            const SizedBox(height: 8),
            FloatingActionButton(
              key: const Key('counterView_decrement_floatingActionButton'),
              child: const Icon(Icons.remove),
              onPressed: () => context.read<TestBloc>().decrement(),
            ),
          ],
        ),
      ),
    );
  }
}

class AppBarTitleWidget extends StatelessWidget {
  final String title;
  final VoidCallback onRefreshPressed;
  const AppBarTitleWidget({
    Key key,
    this.title,
    this.onRefreshPressed,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap: () {
          onRefreshPressed();
        },
        child: Text(title));
  }
}

暂无
暂无

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

相关问题 Flutter 使用不包含 Bloc 类型的上下文调用的 BLoC BlocProvider.of() - Flutter BLoC BlocProvider.of() called with a context that does not contain a Bloc of type BlocProvider.of() 使用不包含 Bloc 的上下文调用 - 即使它包含 - BlocProvider.of() called with a context that does not contain a Bloc - even that it does 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 BlocProvider.of() 调用的上下文不包含 MyBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc 使用不包含 MainBloc 类型的 Bloc 的上下文调用 BlocProvider.of() - BlocProvider.of() called with a context that does not contain a Bloc of type MainBloc 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() 使用不包含 OfflineBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type OfflineBloc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM