简体   繁体   English

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

[英]Error: Type argument 'RoutesBloc' doesn't conform to the bound 'BlocBase<S>' of the type variable 'B' on 'BlocBuilder'

I'm getting this error and I have no clue where it's coming from.我收到了这个错误,我不知道它来自哪里。

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

Full error:完整错误:

lib/screens/home_screen.dart:86:12: Error: Type argument 'RoutesBloc' doesn't conform to the bound 'BlocBase' of the type variable 'B' on 'BlocBuilder'. lib/screens/home_screen.dart:86:12:错误:类型参数“RoutesBloc”不符合“BlocBuilder”上类型变量“B”的绑定“BlocBase”。

  • 'RoutesBloc' is from '/blocs/routes/routes_bloc.dart' ('lib/blocs/routes/routes_bloc.dart'). “RoutesBloc”来自“/blocs/routes/routes_bloc.dart”(“lib/blocs/routes/routes_bloc.dart”)。
  • 'BlocBase' is from 'package:bloc/src/bloc.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/bloc-7.0.0/lib/src/bloc.dart'). 'BlocBase' 来自'package:bloc/src/bloc.dart' ('../../AppData/Local/Pub/Cache/hosted/pub.dartlang.org/bloc-7.0.0/lib/src/块.dart')。 Try changing type arguments so that they conform to the bounds.尝试更改类型 arguments 以使其符合边界。 return BlocBuilder<RoutesBloc, RoutesEvent>( ^返回 BlocBuilder<RoutesBloc, RoutesEvent>( ^

I use a multiplocprovider in my main.dart like this:我在我的main.dart multiplocprovider如下所示:

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

routes_state.dart: 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: 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: 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();
    }
  }
}

I firstly thought that there must be something wrong with my RoutesBloc but changing the blocbuilder to a bloc that I'm successfully using at another place ends up with the same error.我首先认为我的RoutesBloc一定有问题,但是将blocbuilder更改为我在另一个地方成功使用的bloc最终会出现相同的错误。

Does someone know where this is coming from?有人知道这是从哪里来的吗?

It should be return BlocBuilder<RoutesBloc, RoutesState> Check this: https://pub.dev/packages/flutter_bloc#blocbuilder它应该是return BlocBuilder<RoutesBloc, RoutesState>检查这个: https://pub.dev/packages/flutter_bloc#blocbuilder

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

暂无
暂无

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

相关问题 将我的 bloc 应用程序迁移到 null 安全。 错误:“AssignmentBloc”不符合绑定的“BlocBase”<assignmentstate> ' 类型参数 'B'</assignmentstate> - Migrating my bloc app to null safety. error: 'AssignmentBloc' doesn't conform to the bound 'BlocBase<AssignmentState>' of the type parameter 'B' 错误:类型参数“T”不符合“GetIt.call”上类型变量“T”的绑定“对象”。 迁移到 Null 安全后 - Error: Type argument 'T' doesn't conform to the bound 'Object' of the type variable 'T' on 'GetIt.call'. After migrating to Null Safety ( Flutter Bloc ) 不符合绑定的 'StateStreamable<state> ' 类型参数 'B'</state> - ( Flutter Bloc ) doesn't conform to the bound 'StateStreamable<state>' of the type parameter 'B' 参数类型 'BlocBuilder<dynamic, dynamic> '不能分配给参数类型'SliverChildDelegate'</dynamic,> - The argument type 'BlocBuilder<dynamic, dynamic>' can't be assigned to the parameter type 'SliverChildDelegate' Flutter - 错误 BlocProvider void' 不符合界限 - Flutter - Error BlocProvider void' doesn't conform to the bound 错误:参数类型“字符串?” 不能分配给参数类型“字符串”,但 null 检查不起作用 - error: The argument type 'String?' can't be assigned to the parameter type 'String', but null check doesn't work dart class 层次结构错误 - 'a' 不扩展 'b' 尝试使用是或是子类的类型 'b' - dart class hierarchy error - 'a' doesn't extend 'b' Try using a type that is or is a subclass of 'b' &#39;String?&#39; 类型的值不能分配给“字符串”类型的变量。 预期有 1 个位置参数,但找到 0 个 - A value of type 'String?' can't be assigned to a variable of type 'String'. and 1 positional argument(s) expected, but 0 found : 错误: 没有为 class 'BlocBase 定义 getter 'ut'<state> '</state> - : Error: The getter 'ut' isn't defined for the class 'BlocBase<State>' 错误:不能将参数类型“颜色”分配给参数类型“材料颜色”? - error: The argument type 'Color' can't be assigned to the parameter type 'MaterialColor'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM