错误:类型参数“RoutesBloc”不符合“BlocBuilder”上类型变量“B”的绑定“BlocBase<S>”

Posted

技术标签:

【中文标题】错误:类型参数“RoutesBloc”不符合“BlocBuilder”上类型变量“B”的绑定“BlocBase<S>”【英文标题】:Error: Type argument 'RoutesBloc' doesn't conform to the bound 'BlocBase<S>' of the type variable 'B' on 'BlocBuilder' 【发布时间】:2021-07-29 04:17:35 【问题描述】:

我收到了这个错误,但我不知道它是从哪里来的。

class Routes extends StatelessWidget 
  @override
  Widget build(BuildContext context) 
    return BlocBuilder<RoutesBloc, RoutesEvent>( // <-- It occurs here
      builder: (context, state) 
        return Text('...');
      ,
    );
  

完全错误:

lib/screens/home_screen.dart:86:12:错误:类型参数“RoutesBloc”不符合“BlocBuilder”上类型变量“B”的绑定“BlocBase”。

“RoutesBloc”来自“/blocs/routes/routes_bloc.dart”(“lib/blocs/routes/routes_bloc.dart”)。 'BlocBase' 来自'package:bloc/src/bloc.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/bloc-7.0.0/lib /src/bloc.dart')。 尝试更改类型参数,使它们符合界限。 返回 BlocBuilder( ^

我在main.dart 中使用multiplocprovider,如下所示:

MultiBlocProvider(
  providers: [
      ...,
      BlocProvider<RoutesBloc>(
          create: (_) => RoutesBloc(
            apiRepository: ApiRepository.create(),
          )..add(RoutesLoaded()),
        ),
      ],
  child: AppView(),
)

routes_state.dart:

abstract class RoutesState extends Equatable 

const RoutesState();

  @override
  List<Object> get props => [];


class RoutesLoadInProgress extends RoutesState 

class RoutesLoadSuccess extends RoutesState 
  final List<BoulderingRoute> routes;

  const RoutesLoadSuccess([this.routes = const []]);

  @override
  List<Object> get props => [routes];


class RoutesLoadFailure extends RoutesState 

routes_event.dart:

abstract class RoutesEvent extends Equatable 
  const RoutesEvent();

  @override
  List<Object> get props => [];


class RoutesLoaded extends RoutesEvent 

class RouteAdded extends RoutesEvent 
  final BoulderingRoute route;

  const RouteAdded(this.route) : assert(route != null);

  @override
  List<Object> get props => [route];


class RouteUpdated extends RoutesEvent 
  final BoulderingRoute route;

  const RouteUpdated(this.route) : assert(route != null);

  @override
  List<Object> get props => [route];


class RouteDeleted extends RoutesEvent 
  final BoulderingRoute route;

  const RouteDeleted(this.route) : assert(route != null);

  @override
  List<Object> get props => [route];

routes_bloc.dart:

class RoutesBloc extends Bloc<RoutesEvent, RoutesState> 
  final ApiRepository _apiRepository;

  RoutesBloc(ApiRepository apiRepository)
      : assert(apiRepository != null),
        this._apiRepository = apiRepository,
        super(RoutesLoadInProgress());

  @override
  Stream<RoutesState> mapEventToState(
    RoutesEvent event,
  ) async* 
    print(event);
    if (event is RoutesLoaded) 
      yield* _mapRoutesLoadedToState();
    
  

  Stream<RoutesState> _mapRoutesLoadedToState() async* 
    try 
      print('start');
      final List<BoulderingRoute> routes =
          await _apiRepository.fetchBoulderingRoutes();
      yield RoutesLoadSuccess(routes);
     catch (_) 
      yield RoutesLoadFailure();
    
  

我首先认为我的RoutesBloc 一定有问题,但是将blocbuilder 更改为我在另一个地方成功使用的bloc 最终会出现同样的错误。

有人知道这是从哪里来的吗?

【问题讨论】:

【参考方案1】:

应该是return BlocBuilder&lt;RoutesBloc, RoutesState&gt; 检查这个:https://pub.dev/packages/flutter_bloc#blocbuilder

 BlocBuilder<BlocA, BlocAState>(
   builder: (context, state) 
     // return widget here based on BlocA's state
   
 )

【讨论】:

我对自己的愚蠢印象深刻。我一遍又一遍地看了这个,但没有看到明显的:/。谢谢@Luis A. Chaglla。

以上是关于错误:类型参数“RoutesBloc”不符合“BlocBuilder”上类型变量“B”的绑定“BlocBase<S>”的主要内容,如果未能解决你的问题,请参考以下文章

参数类型 'Range<Int>' 不符合预期的类型 'Sequence' Swift3

推断类型参数[Boolean]不符合方法过滤器的类型参数bounds [T.

参数类型 '[HKCategoryType?]' 不符合预期的类型 'Hashable'

参数类型 '[HKCategoryType?]' 不符合预期的类型 'Hashable'

Scala 类型参数不符合类型参数范围

参数类型 'SecretSpec' 不符合预期的类型 'Sequence'