3

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
alma elghoul
  • 183
  • 1
  • 2
  • 12

3 Answers3

4

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.

Abbas Jafari
  • 1,492
  • 2
  • 16
  • 28
2

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

Rohan Thacker
  • 5,377
  • 3
  • 21
  • 34
0

You have this error because there is infinite size, you can use SingleChildScrollView.

Yakup
  • 28
  • 4