错误 "Could not find the correct Provider<AppThemeNotifier> above this Consumer<AppThemeNotifier>"

Error "Could not find the correct Provider<AppThemeNotifier> above this Consumer<AppThemeNotifier>"

我目前正在开发 Flutter 应用程序,但在编写测试时遇到了一些问题。

当我 运行 我的测试时,我有以下异常: “以下 ProviderNotFoundException 被抛出构建消费者(脏): 错误:在此消费者之上找不到正确的提供者 小工具

这是我的main.dart

Widget build(BuildContext context) {
    themeData = Theme.of(context);
    return Consumer<AppThemeNotifier>(
      builder: (BuildContext context, AppThemeNotifier value, Widget? child) {
        customAppTheme = AppTheme.getCustomAppTheme(value.themeMode());
        return MultiBlocProvider(
            providers: [
              BlocProvider<AuthentificationBloc>(
                create: (context) => AuthentificationBloc(),
              ),
              BlocProvider<RegisterBloc>(
                create: (context) => RegisterBloc(),
              ),
              BlocProvider<EventsBloc>(
                create: (context) => EventsBloc(),
              ),
              BlocProvider<UsersBloc>(
                create: (context) => UsersBloc(),
              ),
            ],

这是我尝试进行的测试 运行

void main() {

Widget skeleton(widgetToTest)
{
  return Consumer<AppThemeNotifier>(
      builder: (BuildContext context, AppThemeNotifier value, Widget? child) {
        return widgetToTest;
      });

}

  testWidgets('My login page contains two textfield to enter my credentials',
      (WidgetTester tester) async {
    await tester.pumpWidget(skeleton(Login2Screen()));

    final textFormFieldFinder = find.byElementType(TextFormField);

    expect(textFormFieldFinder, findsNWidgets(2));

  });
}

我尝试创建“骨架”函数来添加提供者,但它不起作用...

非常感谢任何帮助:)

您需要通过 ChangeNotifierProvider 或任何其他合适的提供商加载 AppThemeNotifier

await tester.pumpWidget(
  ChangeNotifierProvider<AppThemeProvider>(
    create: (context) => AppThemeProvider(),
    child: skeleton(Login2Screen()),
  ),
);