Erf*_*koo 5 dart flutter flutter-dependencies
我在 Flutter 应用程序中使用了Fluro包,并且我使用处理程序实现了所有路由,例如这个包的示例。您能否让我知道在路由之间传递对象或对象列表的正确方法是什么?
我曾经像这个问题中的所有其他解决方案一样将数据作为编码的 JSON 和字符串格式传递。最近,Fluro插件提供了一种在路由之间传递参数作为类对象的方法,就像 Flutter 导航器一样。
使用自定义推送路由后,RouteSettings您可以使用BuildContext.settings扩展来提取设置。通常,这将完成,Handler.handlerFunc以便您可以传递RouteSettings.arguments到屏幕小部件。
/// Push a route with custom RouteSettings if you don't want to use path params
FluroRouter.appRouter.navigateTo(
context,
'home',
routeSettings: RouteSettings(
arguments: MyArgumentsDataClass('foo!'),
),
);
/// Extract the arguments using [BuildContext.settings.arguments] or [BuildContext.arguments] for short
var homeHandler = Handler(
handlerFunc: (context, params) {
final args = context.settings.arguments as MyArgumentsDataClass;
return HomeComponent(args);
},
);
Run Code Online (Sandbox Code Playgroud)
如果使用 Flutter 导航器而不是 Fluro 插件,请使用此链接或检查以下方法。
导航器提供了使用通用标识符从应用程序的任何部分导航到命名路线的能力。在某些情况下,您可能还需要将参数传递给命名路由。例如,您可能希望导航到/user路线并将有关用户的信息传递到该路线。
要传递不同的数据,请创建一个存储此信息的类。
// You can pass any object to the arguments parameter.
// In this example, create a class that contains a customizable
// title and message.
class ScreenArguments {
final String title;
final String message;
ScreenArguments(this.title, this.message);
}
Run Code Online (Sandbox Code Playgroud)
现在您需要使用ModalRoute定义一个处理程序:
static Handler _screenHandler = Handler(
handlerFunc: (BuildContext context, Map<String, dynamic> params) {
final ScreenArguments args = ModalRoute
.of(context)
.settings
.arguments;
return PassArgumentsScreen(args?.title);
});
Run Code Online (Sandbox Code Playgroud)
并通过以下方式导航:
Navigator.pushNamed(
context,
"/screen",
arguments: ScreenArguments(
'Class Arguments Screen',
'This message is extracted in the build method.',
),
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2669 次 |
| 最近记录: |