路由错误显示缺少一个参数?
Routing error shows missing one argument?
这就是我转到下一页的方式,
'/ot1': (context) => CustomListView(),
它在 TAP 上运行良好
onTap: (){
Navigator.pushNamed(context, '/ot1');
},
但是当我在 class CustomListView
中创建构造函数并传递字段时,我收到此错误,指出此行中缺少一个参数 '/ot1': (context) => CustomListView(),
这是我class CustomListView
下面分享的代码
class CustomListView extends StatelessWidget {
final List<Spacecraft> spacecrafts;
CustomListView(this.spacecrafts);
Widget build(context) {
return ListView.builder(
itemCount: spacecrafts.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(spacecrafts[currentIndex], context);
},
);
}
我已经搜索了很多,但没有找到编程和 FLUTTER 语言的新解决方案,请帮助
如果您想使用 pushedName 在屏幕之间传递数据,请这样做,
Navigator.pushNamed(
context, '/ot1', arguments: mFeedData);
并获取数据,如
@override
Widget build(BuildContext context) {
mFeedData = ModalRoute.of(context).settings.arguments;
....
}
如果您不想传递任何数据,请从您的代码中删除以下部分
CustomListView(this.spacecrafts)
或将其作为可选的位置参数,
CustomListView([this.spacecrafts])
Wrapping a set of function parameters in [] marks them as optional positional parameters:
When calling a function, you can specify named parameters using paramName: value.
For example:
enableFlags(bold: true, hidden: false);
定义函数时,使用{param1, param2, …}
指定命名参数:
/// Sets the [bold] and [hidden] flags ...
void enableFlags({bool bold, bool hidden}) {...}
尝试添加方括号 [this.spacecrafts] 使航天器参数成为可选参数。
class CustomListView extends StatelessWidget {
final List<Spacecraft> spacecrafts;
CustomListView([this.spacecrafts]); # <- this is where you use the brackets
...
我在我的 Flutter 应用程序中使用此解决方案进行导航:
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
// Getting arguments passed in while calling Navigator.pushNamed
final args = settings.arguments;
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => Home());
case '/routeWithoutArguments':
return MaterialPageRoute(
builder: (_) => PageWithoutArguments(),
);
case '/routeWithArguments':
if (args is String) { // I check if the arguments provided are valids
return MaterialPageRoute(
builder: (_) => PageWithArguments(myString: args),
);
}
return _errorRoute();
default:
return _errorRoute();
}
}
static Route<dynamic> _errorRoute() {
return MaterialPageRoute(builder: (_) {
return Scaffold(
appBar: AppBar(
title: Text('Error'),
),
body: Center(
child: Text('ERROR'),
),
);
});
}
}
然后在我的小部件中 App
:
MaterialApp(
initialRoute: '/',
onGenerateRoute: RouteGenerator.generateRoute,
...
)
然后不带参数调用我的路由:
Navigator.pushNamed(
context,
'/routeWithoutArguments',
);
并使用参数调用:
Navigator.pushNamed(
context,
'/routeWithArguments',
arguments: "my argument",
);
那里学到了这个解决方案
这就是我转到下一页的方式,
'/ot1': (context) => CustomListView(),
它在 TAP 上运行良好
onTap: (){
Navigator.pushNamed(context, '/ot1');
},
但是当我在 class CustomListView
中创建构造函数并传递字段时,我收到此错误,指出此行中缺少一个参数 '/ot1': (context) => CustomListView(),
这是我class CustomListView
下面分享的代码
class CustomListView extends StatelessWidget {
final List<Spacecraft> spacecrafts;
CustomListView(this.spacecrafts);
Widget build(context) {
return ListView.builder(
itemCount: spacecrafts.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(spacecrafts[currentIndex], context);
},
);
}
我已经搜索了很多,但没有找到编程和 FLUTTER 语言的新解决方案,请帮助
如果您想使用 pushedName 在屏幕之间传递数据,请这样做,
Navigator.pushNamed(
context, '/ot1', arguments: mFeedData);
并获取数据,如
@override
Widget build(BuildContext context) {
mFeedData = ModalRoute.of(context).settings.arguments;
....
}
如果您不想传递任何数据,请从您的代码中删除以下部分
CustomListView(this.spacecrafts)
或将其作为可选的位置参数,
CustomListView([this.spacecrafts])
Wrapping a set of function parameters in [] marks them as optional positional parameters:
When calling a function, you can specify named parameters using paramName: value. For example:
enableFlags(bold: true, hidden: false);
定义函数时,使用{param1, param2, …}
指定命名参数:
/// Sets the [bold] and [hidden] flags ...
void enableFlags({bool bold, bool hidden}) {...}
尝试添加方括号 [this.spacecrafts] 使航天器参数成为可选参数。
class CustomListView extends StatelessWidget {
final List<Spacecraft> spacecrafts;
CustomListView([this.spacecrafts]); # <- this is where you use the brackets
...
我在我的 Flutter 应用程序中使用此解决方案进行导航:
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
// Getting arguments passed in while calling Navigator.pushNamed
final args = settings.arguments;
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => Home());
case '/routeWithoutArguments':
return MaterialPageRoute(
builder: (_) => PageWithoutArguments(),
);
case '/routeWithArguments':
if (args is String) { // I check if the arguments provided are valids
return MaterialPageRoute(
builder: (_) => PageWithArguments(myString: args),
);
}
return _errorRoute();
default:
return _errorRoute();
}
}
static Route<dynamic> _errorRoute() {
return MaterialPageRoute(builder: (_) {
return Scaffold(
appBar: AppBar(
title: Text('Error'),
),
body: Center(
child: Text('ERROR'),
),
);
});
}
}
然后在我的小部件中 App
:
MaterialApp(
initialRoute: '/',
onGenerateRoute: RouteGenerator.generateRoute,
...
)
然后不带参数调用我的路由:
Navigator.pushNamed(
context,
'/routeWithoutArguments',
);
并使用参数调用:
Navigator.pushNamed(
context,
'/routeWithArguments',
arguments: "my argument",
);
那里学到了这个解决方案