简体   繁体   中英

Error: Could not find the correct Provider<ThemeNotifier> above this Consumer<ThemeNotifier> Widget

I want to use Dark/Light theme with Switch inside Drawer. You can see "Switch" in my "drawerDosyasi.dart". But firstly i wanted to write codes with SwitchListTile at my HomePage(Anasayfa).

Two Questions:

1- I have this error now

Error: Could not find the correct Provider above this Consumer Widget

This likely happens because you used a BuildContext that does not include the provider of your choice. There are a few common scenarios:

  • The provider you are trying to read is in a different route.

    Providers are "scoped". So if you insert of provider inside a route, then other routes will not be able to access that provider.

  • You used a BuildContext that is an ancestor of the provider you are trying to read.

    Make sure that Consumer is under your MultiProvider/Provider. This usually happen when you are creating a provider and trying to read it immediately.

    For example, instead of:

     Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // Will throw a ProviderNotFoundError, because `context` is associated // to the widget that is the parent of `Provider<Example>` child: Text(context.watch<Example>()), ), }

    consider using builder like so:

     Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // we use `builder` to obtain a new `BuildContext` that has access to the provider builder: (context) { // No longer throws return Text(context.watch<Example>()), } ), }

2- I cant move Switch to Drawer. Gives error like about turning null. So i want to able to change dark/light mode with Switch() in DrawerHeader

Can anyone fix my codes? I'm new and can't solve the problem for two days.

anasayfa.dart

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => ThemeNotifier(),
      child: Consumer<ThemeNotifier>(
        builder: (context, ThemeNotifier notifier, child) {
          return MaterialApp(
            title: 'Flutter Theme Provider',
            theme: notifier.darkTheme ? dark : light,
            home: Anasayfa(),
          );
        },
      ),
    );
  }
}

class Anasayfa extends StatefulWidget {
  @override
  _AnasayfaState createState() => _AnasayfaState();
}

class _AnasayfaState extends State<Anasayfa> {
  int currentPage = 0;

  nested() {
    return NestedScrollView(
      headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
        return [
          SliverAppBar(
            toolbarHeight: 40,
            expandedHeight: 92.0,
            floating: false,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              background: Image.asset(
                "assets/images/besmele.jpg",
                fit: BoxFit.cover,
              ),
            ),
            actions: [
              IconButton(
                icon: Icon(Icons.account_circle),
                color: Colors.white,
                onPressed: () {},
              ),
            ],
          )
        ];
      },
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Consumer<ThemeNotifier>(
            builder: (context, notifier, child) => SwitchListTile(
              title: Text("Dark Mode"),
              onChanged: (val) {
                notifier.toggleTheme();
              },
              value: notifier.darkTheme,
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: nested(),
      drawer: Drawer(
        child: DrawerDosyasi(),
      ),
      bottomNavigationBar: CurvedNavigationBar(
        color: Colors.blue,
        backgroundColor: Colors.white,
        buttonBackgroundColor: Colors.blue,
        height: 50,
        items: <Widget>[
          Icon(
            Icons.campaign,
            size: 20,
            color: Colors.white,
          ),
          Icon(
            Icons.supervisor_account,
            size: 20,
            color: Colors.white,
          ),
          Icon(
            Icons.home,
            size: 20,
            color: Colors.white,
          ),
          Icon(
            Icons.video_collection_rounded,
            size: 20,
            color: Colors.white,
          ),
          Icon(
            Icons.menu_book_rounded,
            size: 20,
            color: Colors.white,
          ),
        ],
        animationDuration: Duration(
          milliseconds: 300,
        ),
        index: 2,
        animationCurve: Curves.bounceInOut,
        onTap: (index) {
          debugPrint("Current index is $index");
        },
      ),
    );
  }
}

drawerDosyasi.dart

    class DrawerDosyasi extends StatefulWidget {
  @override
  _DrawerDosyasiState createState() => _DrawerDosyasiState();
}

class _DrawerDosyasiState extends State<DrawerDosyasi> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          DrawerHeader(
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
            child: Row(
              children: [
                Expanded(
                  flex: 1,
                  child: Switch(),
                ),
                Expanded(
                  flex: 3,
                  child: CircleAvatar(
                    radius: 60,
                    backgroundColor: Colors.white,
                  ),
                ),
                Expanded(flex: 1, child: SizedBox.expand()),
              ],
            ),
          ),
          ListTile(
            leading: Icon(Icons.home),
            title: Text('Anasayfa'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.account_circle),
            title: Text('Profil'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.campaign),
            title: Text('Duyurular'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.account_box),
            title: Text('Hocalar'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.library_books),
            title: Text('Dergiler'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.video_collection_rounded),
            title: Text('Canlı Dersler'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.menu_book_rounded),
            title: Text('Kütüphane'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.supervisor_account),
            title: Text('Tartışmalar'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.create),
            title: Text('Yazı Gönder'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.message_rounded),
            title: Text('İletişim'),
            tileColor: Colors.white,
          ),
          ListTile(
            leading: Icon(Icons.exit_to_app_rounded),
            title: Text('Çıkış'),
            tileColor: Colors.white,
          ),
        ],
      ),
    );
  }
}

theme.dart

ThemeData light = ThemeData(
    brightness: Brightness.light,
    primarySwatch: Colors.blue,
    accentColor: Colors.blue,
    scaffoldBackgroundColor: Color(0xfff1f1f1));

ThemeData dark = ThemeData(
  brightness: Brightness.dark,
  primarySwatch: Colors.indigo,
  accentColor: Colors.green[700],
);

class ThemeNotifier extends ChangeNotifier {
  final String key = "theme";
  SharedPreferences _prefs;
  bool _darkTheme;

  bool get darkTheme => _darkTheme;

  ThemeNotifier() {
    _darkTheme = true;
    _loadFromPrefs();
  }

  toggleTheme() {
    _darkTheme = !_darkTheme;
    _saveToPrefs();
    notifyListeners();
  }

  _initPrefs() async {
    if (_prefs == null) _prefs = await SharedPreferences.getInstance();
  }

  _loadFromPrefs() async {
    await _initPrefs();
    _darkTheme = _prefs.getBool(key) ?? true;
    notifyListeners();
  }

  _saveToPrefs() async {
    await _initPrefs();
    _prefs.setBool(key, _darkTheme);
  }
}
  1. The Switch value parameter is required, and onChanged too:

    Switch(value: false, onChanged: (value) {/* do something with new value*/})

  1. You need to match the type parameter for both Consumer<T> and ChangeNotifierProvider<T>
Widget build(BuildContext context) {
    return ChangeNotifierProvider<ThemeNotifier>() 
...
  1. You need to explain more about your Switch(), what exactly you wan't to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM