错误:无法将'AppStateNotifier'类型的值分配给'Widget类型的变量

时间:2020-08-04 20:24:58

标签: flutter dart themes light android-dark-theme

遵循本教程。 https://itnext.io/app-theming-in-flutter-dark-mode-light-mode-27d9adf3cee

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  runApp(
    ChangeNotifierProvider<AppStateNotifier>(
      builder: (context) => AppStateNotifier(), //<--COMPILER ERROR, details below.
      child: MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return Consumer<AppStateNotifier>(
      builder: (context, appState, child) {
        return MaterialApp(
          title: 'Lockify',
          debugShowCheckedModeBanner: false,
          theme: AppTheme.lightTheme, 
          darkTheme:
              AppTheme.darkTheme, 
          home: MyHomePage(),
          themeMode: appState.isDarkModeOn ? ThemeMode.dark : ThemeMode.light,
        );
      },
    );
  }
}

错误: 编译器消息: lib / main.dart:16:29:错误:无法将类型为“ AppStateNotifier”的值分配给类型为“小部件”的变量。

  • “ AppStateNotifier”来自“ package:lockify / appstatenotifier.dart”(“ lib / appstatenotifier.dart”)。
  • “小部件”来自“ package:flutter / src / widgets / framework.dart”(“ ../../ Developer / flutter / packages / flutter / lib / src / widgets / framework.dart”)。 构建器:(上下文)=> AppStateNotifier(),

lib / main.dart:16:16:错误:参数类型'Widget Function(BuildContext)'无法分配给参数类型'Widget Function(BuildContext,Widget)'。

  • “小部件”来自“ package:flutter / src / widgets / framework.dart”(“ ../../ Developer / flutter / packages / flutter / lib / src / widgets / framework.dart”)。 / li>
  • “ BuildContext”来自“ package:flutter / src / widgets / framework.dart”(“ ../../ Developer / flutter / packages / flutter / lib / src / widgets / framework.dart”)。 构建器:(上下文)=> AppStateNotifier(),

1 个答案:

答案 0 :(得分:0)

尝试在builder小部件上将create更改为ChangeNotifierProvider