3
votes

Following this tutorial. 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,
        );
      },
    );
  }
}

ERROR: Compiler message: lib/main.dart:16:29: Error: A value of type 'AppStateNotifier' can't be assigned to a variable of type 'Widget'.

  • 'AppStateNotifier' is from 'package:lockify/appstatenotifier.dart' ('lib/appstatenotifier.dart').
  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../../Developer/flutter/packages/flutter/lib/src/widgets/framework.dart'). builder: (context) => AppStateNotifier(),

lib/main.dart:16:16: Error: The argument type 'Widget Function(BuildContext)' can't be assigned to the parameter type 'Widget Function(BuildContext, Widget)'.

  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('../../Developer/flutter/packages/flutter/lib/src/widgets/framework.dart').
  • 'BuildContext' is from 'package:flutter/src/widgets/framework.dart' ('../../Developer/flutter/packages/flutter/lib/src/widgets/framework.dart'). builder: (context) => AppStateNotifier(),
1

1 Answers

5
votes

Try changing builder to create on the ChangeNotifierProvider widget.