错误:“AppStateNotifier”类型的值不能分配给“Widget”类型的变量
Posted
技术标签:
【中文标题】错误:“AppStateNotifier”类型的值不能分配给“Widget”类型的变量【英文标题】:Error: A value of type 'AppStateNotifier' can't be assigned to a variable of type 'Widget 【发布时间】:2020-11-25 00:05:00 【问题描述】:按照本教程。 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”来自“package:lockify/appstatenotifier.dart”(“lib/appstatenotifier.dart”)。 'Widget' 来自'package:flutter/src/widgets/framework.dart' ('../../Developer/flutter/packages/flutter/lib/src/widgets/framework.dart')。 builder: (context) => AppStateNotifier(),lib/main.dart:16:16: 错误:无法将参数类型“Widget Function(BuildContext)”分配给参数类型“Widget Function(BuildContext, Widget)”。
'Widget' 来自'package:flutter/src/widgets/framework.dart' ('../../Developer/flutter/packages/flutter/lib/src/widgets/framework.dart')。 “BuildContext”来自“package:flutter/src/widgets/framework.dart”(“../../Developer/flutter/packages/flutter/lib/src/widgets/framework.dart”)。 builder: (context) => AppStateNotifier(),【问题讨论】:
【参考方案1】:尝试在ChangeNotifierProvider
小部件上将builder
更改为create
。
【讨论】:
以上是关于错误:“AppStateNotifier”类型的值不能分配给“Widget”类型的变量的主要内容,如果未能解决你的问题,请参考以下文章
错误“[User]类型的值'无法转换为预期的参数类型'User”错误的解决方案是啥?
无法转换类型的值'(数据:CMAccelerometerData!,错误:NSError!)
SwiftUI 错误:无法转换“UIImage”类型的值?关闭结果类型“无效”