I have used Fluro package inside my Flutter app, and I implemented all of my routes with a handler like the example of this package. Could you please let me know what is the correct way to pass an object or list of objects between routes?
And for that we'll use Navigator widget pushNamed and pass the product ID as an argument. To navigate to a new page we must define the routes first in our MaterialApp widget. Before that, we need to declare a static constant string as routename in the Shop Product Detail Screen widget.
Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions. Features. Simple route navigation. Function handlers [map to a function instead of a route] Wildcard parameter matching.
You could do that via ModalRoute.of(context).settings.arguments
so if you have object:
class ScreenArguments {
final String title;
ScreenArguments(this.title);
}
navigate with:
Navigator.of(context).pushNamed("/screen", arguments: ScreenArguments("Title"));
and in handler:
static Handler _screenHandler = Handler(handlerFunc: (BuildContext context, Map<String, dynamic> params) {
final ScreenArguments args = ModalRoute.of(context).settings.arguments;
return PassArgumentsScreen(args?.title);
});
The solution presented at How to pass JSON response to another view works unless your object has a string (or the decoded JSON contains) a /
as @Chirag said.
I'm giving my solution as it worked, since my object has an URL inside.
_router.define("/groups/:group", handler: Handler(handlerFunc: (context, params) {
String param = params["group"][0];
GroupAPIModel group = GroupAPIModel.fromJson(decodeJsonDataForFluro(param));
return GroupDetailsScreen(group);
}));
String encodeJsonDataForFluro(Map<String, dynamic> mapToEncode) {
return jsonEncode(mapToEncode).replaceAll("/", HtmlEscape().convert("/"));
}
Map<String, dynamic> decodeJsonDataForFluro(String encodedMap) {
return jsonDecode(encodedMap.replaceAll(HtmlEscape().convert("/"), "/"));
}
void _onGroupClicked(GroupAPIModel group) {
String bodyJson = encodeJsonDataForFluro(group.toJson());
router.navigateTo(context: context, path: "/groups/$bodyJson");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With