简体   繁体   English

BlocProvider.of() 使用不包含 CounterBloc 类型的 Cubit 的上下文调用

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

The following is a example code from :以下是来自的示例代码:

https://bloclibrary.dev/#/flutterbloccoreconcepts https://bloclibrary.dev/#/flutterbloccoreconcepts

I am getting the following exception,我收到以下异常,

How can I fix the following Exception and why is this happening:如何修复以下异常以及为什么会发生这种情况:

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ ========小部件库捕获的异常===================================== ==================

The following assertion was thrown building CounterPage(dirty):构建 CounterPage(dirty) 时抛出了以下断言:

BlocProvider.of() called with a context that does not contain a Cubit of type CounterBloc. BlocProvider.of() 使用不包含 CounterBloc 类型的 Cubit 的上下文调用。

No ancestor could be found starting from the context that was passed to BlocProvider.of().从传递给 BlocProvider.of() 的上下文开始,找不到任何祖先。

This can happen if the context you used comes from a widget above the BlocProvider.如果您使用的上下文来自 BlocProvider 上方的小部件,则可能会发生这种情况。

The context used was: CounterPage(dirty)使用的上下文是: CounterPage(dirty)

main.dart main.dart

import 'package:flutter/material.dart';

import 'counter_page.dart';

void main() {
  runApp(MyApp());
} 
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Bloc Counter Example',
      home: CounterPage(),
    );
  }
}

counter_page.dart counter_page.dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import'package:flutterbloc_counter_app/counter_bloc.dart';

class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final CounterBloc counterBloc = BlocProvider.of<CounterBloc>(context);

    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: BlocBuilder<CounterBloc, int>(
        builder: (context, count) {
          return Center(
            child: Text(
              '$count',
              style: TextStyle(fontSize: 24.0),
            ),
          );
        },
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                counterBloc.add(CounterEvent.increment);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.remove),
              onPressed: () {
                counterBloc.add(CounterEvent.decrement);
              },
            ),
          ),
        ],
      ),
    );
  }
}

counter_bloc.dart counter_bloc.dart

import 'package:flutter_bloc/flutter_bloc.dart';
    
enum CounterEvent { increment, decrement }

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0);

  @override
  Stream<int> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        yield state - 1;
        break;
      case CounterEvent.increment:
        yield state + 1;
        break;
    }
  }
}

You can not use 'BlocProvider.of' without BlocProvider at the parent.在父级没有 BlocProvider 的情况下,您不能使用 'BlocProvider.of'。
You just create a 'CounterBloc' and pass the bloc to BlocBuilder.您只需创建一个“CounterBloc”并将该块传递给 BlocBuilder。

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import'package:flutterbloc_counter_app/counter_bloc.dart';

class CounterPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final CounterBloc counterBloc = CounterBloc();

    return Scaffold(
      appBar: AppBar(title: Text('Counter')),
      body: BlocBuilder<CounterBloc, int>(
        bloc: counterBloc,
        builder: (context, count) {
          return Center(
            child: Text(
              '$count',
              style: TextStyle(fontSize: 24.0),
            ),
          );
        },
      ),
      floatingActionButton: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () {
                counterBloc.add(CounterEvent.increment);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(vertical: 5.0),
            child: FloatingActionButton(
              child: Icon(Icons.remove),
              onPressed: () {
                counterBloc.add(CounterEvent.decrement);
              },
            ),
          ),
        ],
      ),
    );
  }
}

It looks like I forgot to provide an instance of the bloc to the CounterPage.看起来我忘记向 CounterPage 提供该集团的一个实例。 Wrapping the CounterPage widget in a BlocProvider and create the instance of the bloc like this will do the trick:将 CounterPage 小部件包装在 BlocProvider 中并像这样创建 bloc 的实例就可以解决问题:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Bloc Counter Example',
      home: BlocProvider(
        create: (context) => CounterBloc(),
        child: CounterPage(),
      ),
    );
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循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 BlocProvider.of() 使用不包含 Cubit 类型的上下文调用 - BlocProvider.of() called with a context that does not contain a Cubit of type BlocProvider.of() 调用的上下文不包含 MyBloc 类型的 Bloc/Cubit - BlocProvider.of() called with a context that does not contain a Bloc/Cubit of type MyBloc 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() 使用不包含 OfflineBloc 类型的 Bloc 的上下文调用 - BlocProvider.of() called with a context that does not contain a Bloc of type OfflineBloc Flutter:使用不包含 Bloc 类型的上下文调用 blocprovider.of() - Flutter: blocprovider.of() called with a context that does not contain a Bloc of type 使用不包含 Bloc 类型的上下文调用 BlocProvider.of() - 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM