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

e1xvtsh3  于 12个月前  发布在  Flutter
关注(0)|答案(1)|浏览(153)
@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,
        ),
      )
    );
  }

字符串
这是主要的dart文件导致的错误

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;
  }
}


这是所有供应商所在的地方
我尝试的是:
我尝试将返回类型dynamic更改为BlocProvider
我将providers属性更改为

AppPages.allBlocProviders(context) to  [...AppPages.allBlocProviders(context)].

km0tfn4u

km0tfn4u1#

你不应该在allBlocProviders函数中使用dynamic,所以你写的三个地方都应该写dynamic,你应该写BlocProvider,并检查块是否为null,比如

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;

相关问题