无法将参数类型“List<dynamic>”分配给参数类型“List<SingleChildWidget>

The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<SingleChildWidget>'

提问人:Karthikn 提问时间:11/16/2023 最后编辑:nageenKarthikn 更新时间:11/17/2023 访问量:55

问:

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: AppPages.allBlocProviders(context) ,
      child: ScreenUtilInit(
      builder: (context, child) =\>  MaterialApp(
          debugShowCheckedModeBanner: false,
          home: const Welcome(),
          theme: ThemeData(
            appBarTheme: const AppBarTheme(
              iconTheme: IconThemeData(
                color: AppColors.primaryText
              ),
              elevation: 0,
              backgroundColor: Colors.white
            )
          ),
          onGenerateRoute: AppPages.GenerateRouteSettings,
        ),
      )
    );
  }

这是主要的飞镖文件导致的错误

class PageEntity{
  String route;
  Widget page;
  dynamic bloc;

  PageEntity({required this.route, required this.page,  this.bloc});
}

这是帮助循环访问提供程序的实体类

class AppPages{

  static List<PageEntity> routes (){
    return [
      PageEntity(
      route: AppRoutes.INITIAL, 
      page: const Welcome(), 
      bloc: BlocProvider(create: (_)=>WelcomeBloc(),)
    ),
      PageEntity(
      route: AppRoutes.SING_IN, 
      page: const SignInPage(), 
      bloc: BlocProvider(create: (_)=>SignInBlocs(),)
    ),
      PageEntity(
      route: AppRoutes.REGISTER, 
      page: const Register(), 
      bloc: BlocProvider(create: (_)=>RegisterBlocs(),)
    ),
      PageEntity(
      route: AppRoutes.APPLICATION, 
      page: const Welcome(), 
    ),
    ];
  }


// return all the bloc providers
   static List<dynamic> allBlocProviders(BuildContext context){
    List<dynamic> blocProviders = <dynamic>[];
    for(var bloc in routes()){
    
        blocProviders.add(bloc.bloc);
      
    }
    return blocProviders;
  }class AppPages{

  static List<PageEntity> routes (){
    return [
      PageEntity(
      route: AppRoutes.INITIAL, 
      page: const Welcome(), 
      bloc: BlocProvider(create: (_)=>WelcomeBloc(),)
    ),
      PageEntity(
      route: AppRoutes.SING_IN, 
      page: const SignInPage(), 
      bloc: BlocProvider(create: (_)=>SignInBlocs(),)
    ),
      PageEntity(
      route: AppRoutes.REGISTER, 
      page: const Register(), 
      bloc: BlocProvider(create: (_)=>RegisterBlocs(),)
    ),
      PageEntity(
      route: AppRoutes.APPLICATION, 
      page: const Welcome(), 
    ),
    ];
  }


// return all the bloc providers
   static List<dynamic> allBlocProviders(BuildContext context){
    List<dynamic> blocProviders = <dynamic>[];
    for(var bloc in routes()){
    
        blocProviders.add(bloc.bloc);
      
    }
    return blocProviders;
  }
} 

这是所有提供商所在的地方

我试过了什么:

我尝试将返回类型动态更改为 .BlocProvider

我将 providers 属性更改为

AppPages.allBlocProviders(context) to  [...AppPages.allBlocProviders(context)].
flutter dart flutter-dependencies bloc 类型不匹配

评论


答:

2赞 Ivo 11/16/2023 #1

你不应该在函数中使用,所以你写的所有三个地方都应该放进去,并检查块是否不为空,比如dynamicallBlocProvidersdynamicBlocProvider

static List<BlocProvider> allBlocProviders(BuildContext context){
  List<BlocProvider> blocProviders = <BlocProvider>[];
  for (var bloc in routes()){
    if (bloc.bloc != null) {
      blocProviders.add(bloc.bloc!);
    }
  }
  return blocProviders;
}

也在变化中PageEntity

dynamic bloc;

BlocProvider? bloc;