Flutter Invalid argument: Maximum call stack size exceeded

4,173

Solution 1

You have this error because there is an infinite loop with FeedScreen

Solution 2

It seems you called FeedScreen inside itself at this line:

final List<Widget> _widgetOptions = [FeedScreen(), ProgramScreen()];

It can be one of the reason that this problem happened. Please don't use FeedScreen() inside itself and use other classes instead.

Share:
4,173
alma elghoul
Author by

alma elghoul

Updated on December 30, 2022

Comments

  • alma elghoul
    alma elghoul over 1 year

    I am having an exception that I don't understand in my Flutter app. Here is the code :

    main.dart:

    void main() {
      runApp(MyApp());
    }
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Qatar22',
          debugShowCheckedModeBanner: false,
          theme: new ThemeData(
              accentColor: Colors.black,
              indicatorColor: Colors.black,
              highlightColor: Colors.black,
              bottomAppBarColor: Colors.black,
              primaryColor: Color(0xFFffffff),
              primaryColorDark: Color(0xffffff)),
          home: SplashScreen(),
          routes: <String, WidgetBuilder>{
            SPLASH_SCREEN: (BuildContext context) => SplashScreen(),
            FEED_SCREEN: (BuildContext context) => FeedScreen(),
            PROGRAM_SCREEN: (BuildContext context) => ProgramScreen(),
          },
        );
      }
    }
    

    FeedScreen:

    class FeedScreen extends StatefulWidget {
      @override
      State<FeedScreen> createState() => _FeedScreenState();
    }
    
    /// This is the private State class that goes with MyStatefulWidget.
    class _FeedScreenState extends State<FeedScreen> {
      int _selectedIndex = 0;
      final List<Widget> _widgetOptions = [FeedScreen(), ProgramScreen()];
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Gallery'),
          ),
          body: Center(
            child: _widgetOptions.elementAt(_selectedIndex),
          ),
          bottomNavigationBar: BottomNavigationBar(
              items: [
                BottomNavigationBarItem(
                  icon: Icon(CupertinoIcons.news_solid),
                  label: 'Feed',
                ),
                BottomNavigationBarItem(
                  icon: Icon(CupertinoIcons.sportscourt_fill),
                  label: 'Program',
                )
              ],
              currentIndex: _selectedIndex,
              selectedItemColor: Colors.red.shade900,
              onTap: (index) {
                switch (index) {
                  case 0:
                    Navigator.pushNamed(context, FEED_SCREEN);
                    break;
                  case 1:
                    Navigator.pushNamed(context, PROGRAM_SCREEN);
                    break;
                }
              }),
        );
      }
    }
    

    and I am getting this exception:

    The following JSRangeError was thrown building NotificationListener<LayoutChangedNotification>:
    Invalid argument: Maximum call stack size exceeded

    The relevant error-causing widget was:
    Scaffold file:///home/project/lib/screens/feed/feed-screen.dart:21:12

    Would you help me please as I am new to Flutter.

    • Rohan Thacker
      Rohan Thacker almost 3 years
      Please add more info about the error message, the code provided above is not enough to know where the problem occurs.
    • alma elghoul
      alma elghoul almost 3 years
      i added more code
    • Rohan Thacker
      Rohan Thacker almost 3 years
      Thank you, its clear now. Have you been able to solve the issue?
    • Abbas Jafari
      Abbas Jafari almost 3 years
      Did you get your answer?