Flutter 无效参数:超出最大调用堆栈大小
Flutter Invalid argument: Maximum call stack size exceeded
我在 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;
}
}),
);
}
}
我遇到了这个异常:
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
你能帮帮我吗,我是 Flutter 的新手。
出现此错误是因为 FeedScreen
存在无限循环
你似乎在这一行内部调用了 FeedScreen
:
final List<Widget> _widgetOptions = [FeedScreen(), ProgramScreen()];
这可能是出现此问题的原因之一。
请不要在其内部使用 FeedScreen()
,而应使用其他 类。
我在 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;
}
}),
);
}
}
我遇到了这个异常:
The following JSRangeError was thrown building NotificationListener<LayoutChangedNotification>:
Invalid argument: Maximum call stack size exceededThe relevant error-causing widget was:
Scaffold file:///home/project/lib/screens/feed/feed-screen.dart:21:12
你能帮帮我吗,我是 Flutter 的新手。
出现此错误是因为 FeedScreen
你似乎在这一行内部调用了 FeedScreen
:
final List<Widget> _widgetOptions = [FeedScreen(), ProgramScreen()];
这可能是出现此问题的原因之一。
请不要在其内部使用 FeedScreen()
,而应使用其他 类。