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

70 Views Asked by At
  @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,
        ),
      )
    );
  }

This is main dart file cause the error

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

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

This is entity class that help to iterate through the providers

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

This is where all the providers lies

what I tried:

I tried to change the return type dynamic to BlocProvider.

I changed the providers property to

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

There are 1 best solutions below

0
Ivo On BEST ANSWER

You shouldn't use dynamic in the allBlocProviders function so all three places you have written dynamic you should put BlocProvider and also check if the bloc isn't null, like

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

and also in the PageEntity change

dynamic bloc;

to

BlocProvider? bloc;