【问题标题】:Try to internationalize flutter application with BlocProvider尝试使用 BlocProvider 国际化 Flutter 应用程序
【发布时间】:2019-09-19 23:39:41
【问题描述】:

我试图将我所有与语言更改相关的逻辑整合到一个块中。我正在使用 flutter_bloc 包,它可以让我访问“BlocProvider”方法。但是,当我尝试扩展我的块时出现错误。

这是错误:

Error: Type argument 'TranslationsBloc' doesn't conform to
the bound 'Bloc<dynamic, dynamic>' of the type variable 'T' on 'BlocProvider'.

代码如下:

main.dart

return BlocProvider<TranslationsBloc>(
      bloc: translationsBloc,
      child: StreamBuilder<String>(
        ...

MainBloc.dart

abstract class BlocBase {
  void dispose();
}

class TranslationsBloc implements BlocBase {
   ...

【问题讨论】:

    标签: flutter bloc


    【解决方案1】:

    BlocProvider 将自动识别子 bloc 的类型,无需明确提及,因此删除提供给它的类型将解决您的问题:

      return BlocProvider(
        bloc: translationsBloc,
        child: StreamBuilder<String>(
        ...
    

    【讨论】:

    • 是的,我试过了,但我收到了这条消息:'TranslationsBloc' doesn't extend 'Bloc'. Try using a type that is or is a subclass of 'Bloc'.dart(type_argument_not_matching_bounds) Couldn't infer type parameter 'T'. Tried to infer 'TranslationsBloc' for 'T' which doesn't work: Type parameter 'T' declared to extend 'Bloc&lt;dynamic, dynamic&gt;'. The type 'TranslationsBloc' was inferred from: Parameter 'bloc' declared as 'T'
    • 你还需要扩展BlocBase而不是实现它,我在我的个人项目中使用了这种方法,并且运行顺利。
    • 你可以试试这个class LanguageBloc extends Bloc&lt;LanguageEvent, LanguageState&gt; 并添加语言事件和语言状态来维护状态
    猜你喜欢
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多