Stack Overflow Error in Flutter utilizing ChangeNotifierProvider?
Asked Answered
D

2

7

I am receiving the following error -

I/flutter (18695): The following StackOverflowError was thrown building Consumer(dirty, dependencies:

I/flutter (18695): [_DefaultInheritedProviderScope]):

I/flutter (18695): Stack Overflow

This seems to relate to error in my Consumer. I am using Provider plugin to try to create a toggle button for Dark Mode in Flutter.

See below for my files -

appstatenotifier.dart

import 'package:flutter/material.dart'; 
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';


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

ThemeData dark = ThemeData(
  brightness: Brightness.dark, 
  primarySwatch: Colors.indigo, 
  accentColor: Colors.pink,
); 

class ThemeNotifier with ChangeNotifier {
  final String key = "theme"; 
  SharedPreferences prefs; 
  bool _darkTheme; 

  bool get darkTheme => darkTheme; 

  ThemeNotifier() {
    _darkTheme = false; 
  }

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

}

Below is my main.dart relevant Widgets

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    var themeData = ThemeData(
      primarySwatch: Colors.blue,
    );
    return ChangeNotifierProvider(
      create: (_) => ThemeNotifier(), 
        child: Consumer<ThemeNotifier>(
          builder: (context, ThemeNotifier notifier, child) {
            return MaterialApp(
              theme: notifier.darkTheme ? dark : light,
              title: 'Work Out Log',
              routes: MyApp.routes,
            );
          }
        ),
      );
    }
  }

  Widget buildDrawer(context) {
    return Drawer(
      child: ListView(
        children: <Widget>[
          ListTile(
            title: Text('Dark Theme'),
            trailing: Consumer<ThemeNotifier>(
              builder: (context, notifier, child) => SwitchListTile(
                title: Text("Dark mode"), 
                onChanged: (val) {
                  notifier.toggleTheme(); 
                }, 
                value: notifier.darkTheme,
              ),
            ),
          ),
        ],
      ),
    ); 
  }

Any idea why it's throwing this error?

Doornail answered 28/2, 2020 at 5:2 Comment(2)
Is that the entire error? What does the stack trace look like for the error? Where are you calling buildDrawer? Stack overflows happen when there is an infinite method loop (either a method calling itself or multiple methods calling each other over and over again without being told to stop at some point), but there doesn't seem to be enough information here to be able to tell where the stack overflow is coming from.Tay
bool get darkTheme => _darkTheme; here _ is missin. May be that is returning something else, which is causing error.Pythoness
T
16

As Viren V Varasadiya pointed out, your getter for darkTheme is incorrect:

bool get darkTheme => darkTheme; 

Presumably, you meant to point it at _darkTheme, but instead you have the getter that is returning itself. This means that any time you call darkTheme, the getter looks up the value of darkTheme, which makes the getter look up the value of darkTheme, which makes the getter look up the value of darkTheme, which makes the getter look up the value of darkTheme, which makes the getter look up... (hopefully you get the idea).

You just need to change the getter to return the correct thing:

bool get darkTheme => _darkTheme; 
Tay answered 29/2, 2020 at 0:58 Comment(1)
I spent a good 2 hours trying to work out why I was getting the same error. I only realised I was missing the underscore after reading this post.Dearr
R
1

You Must be Retruning the same thing as the class name

Rebeca answered 21/11, 2020 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.