上下文不包含块

编程入门 行业动态 更新时间:2024-10-27 11:22:05
本文介绍了上下文不包含块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在以下测试代码中,当单击应用程序栏中的按钮时,我试图将事件发送到TestBloc.我将BlocBuilder向外移动到支架周围.但是,当调用_reloadData方法时,会弹出以下错误

in the following test code, I am trying to send an event to the TestBloc, when a button in the app bar is clicked. I moved the BlocBuilder outwards around the scaffold. But when the _reloadData method is called, then the following error pops up

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

代码

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

推荐答案

您可以在下复制粘贴运行完整代码我使用以下完整代码来模拟这种情况您可以使用 Builder 包装 AppBarTitleWidget 代码段

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

工作演示

完整代码

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

更多推荐

上下文不包含块

本文发布于:2023-11-11 04:53:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577430.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   不包含

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!