It drives me nuts that Flutter example on official documentation page for passing arguments to a named route fails with null-safety turned ON.
Here's the example I am trying to debug. There's a Consumer
building a list view with ListView.builder
which returns the following:
return ListTile(
title: Text(data.items[index].title),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailsScreen(),
settings: RouteSettings(
// arguments: data.items[index] // <- this does not work
arguments: {
'title': data.items[index].title, // <- read below
}
),
),
);
},
);
inside the DetailsScreen I am trying to get the arguments as following:
final ScreenArguments args = ModalRoute.of(context)!.settings.arguments!;
just like as it is described in the official documentation here. But debugger throws me an error:
A value of type 'Object' can't be assigned to a variable of type 'ScreenArguments'.
though I am just walking through the example and can't find the right way. The print(args.runtimeType);
shows that suddenly my arguments are being transformed into unknown yet _InternalLinkedHashMap<String, String>
and thus can't be casted as ScreenArguments
in the example above. The only way I can get the args by not setting the type like this:
final args = ModalRoute.of(context)!.settings.arguments!;
but again it's not possible to get anything from this Object since it does not implement getters of my custom object obviously.
I also tried to convert the passed argument as a String from json-like object, but the arguments are still converted into _InternalLinkedHashMap
. Don't understand where to go further from here...
UPDATE
So I do not know what happened exactly, but from what I see in the performance of Android Studio, it heavily depends on available CPU power as well as memory. I did not clean up the cache, but restarted the MacOS, accordingly all system garbage collectors should have cleaned up some system cache, then restarted the Android Studio and then tried to cast the
arguments: {
'title': menusData.menus[index].title.toString(),
}
as Map like this:
final args = ModalRoute.of(context)!.settings.arguments! as Map;
and suddenly printing the result in the child Widget like this
print(args);
print(args.runtimeType);
print(args['title']);
yeilds the following result:
I/flutter ( 8653): _InternalLinkedHashMap<String, String>
I/flutter ( 8653): my custom string
which is not perfect, because I stull have to work with the `` class, yet I am able to get the passed String argument and use it in the child Widget. IMO this is still not how it is supposed to work...