微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter 无效参数:超出最大调用堆栈大小

如何解决Flutter 无效参数:超出最大调用堆栈大小

我在 Flutter 应用程序中遇到了一个我不理解的异常。 这是代码

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;
            }
          }),);
  }
}

我收到此异常:

构建 NotificationListener 时抛出以下 JSRangeError:
无效参数:超出最大调用堆栈大小

相关的导致错误的小部件是:
脚手架文件:///home/project/lib/screens/Feed/Feed-screen.dart:21:12

你能帮我吗,因为我是 Flutter 的新手。

解决方法

您似乎在这一行内部调用了 FeedScreen

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

这可能是发生此问题的原因之一。 请不要在其内部使用 FeedScreen(),而应使用其他类。

,

出现此错误是因为 FeedScreen 存在无限循环

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。