I am passing data between screens of an app. I have been able to do this successfully with strings but when it comes to sending a List map the Navigator.pushNamed does nothing and doesn't throw any error.
I have my named routes set up like below, in the onGenerateRoute I am taking the settings and passing the arguments to HomePageResultsScreen and ItemPageProfileScreen:
Main.dart:
void main() => runApp(myApp());
class myApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primaryColor: Colors.white,
),
initialRoute: HomePageScreen.id,
onGenerateRoute: (RouteSettings settings) {
var routes = <String, WidgetBuilder>{
HomePageScreen.id: (context) => HomePageScreen(),
AddItemScreen.id: (context) => AddItemScreen(),
AdvertiseScreen.id: (context) => AdvertiseScreen(),
HomePageFilterScreen.id: (context) => HomePageFilterScreen(),
HomePageResultsScreen.id: (context) => HomePageResultsScreen(settings.arguments),
ItemPageProfileScreen.id: (context) => ItemPageProfileScreen(settings.arguments),
ItemPageProfileSuggestUpdateScreen.id: (context) => ItemPageProfileSuggestUpdateScreen(),
ItemPageWhereToBuyAddStoreToDatabaseScreen.id: (context) => ItemPageWhereToBuyAddStoreToDatabaseScreen(),
ItemPageWhereToBuyMapScreen.id: (context) => ItemPageWhereToBuyMapScreen(),
ItemPageWhereToBuyScreen.id: (context) => ItemPageWhereToBuyScreen(),
MenuScreen.id: (context) => MenuScreen(),
NotAvailableScreen.id: (context) => NotAvailableScreen(),
TermsScreen.id: (context) => TermsScreen(),
};
WidgetBuilder builder = routes[settings.name];
return MaterialPageRoute(builder: (ctx) => builder(ctx));
};
When opening the screen I am just sending a string which works fine like this:
Pushing dart file:
onTap: () {Navigator.pushNamed(context, ItemPageProfileScreen.id, arguments: 'myStringData');
},
and receiving it like this:
Receiving dart file:
class ItemPageProfileScreen extends StatefulWidget {
final String argument;
ItemPageProfileScreen(this.argument);
This all works fine. But the problem comes when I try to send a ListMap across.
So I changed my Navigator to push a map like this:
Pushing dart file:
onTap: () {
List<Map<String, String>> categoriesMap;
categoriesMap.add({
"category": '',
"subCategory1": snapshot.data.documents[index].documentID,
"subCategory2": '',
"subCategory3": ''});
Navigator.pushNamed(context, HomePageResultsScreen.id, arguments: categoriesMap);
},
And then I modified the receiving file to receive the list like this:
Receiving dart file:
class HomePageResultsScreen extends StatefulWidget {
List<Map<String, String>> arguments;
HomePageResultsScreen(this.arguments);
But when I try to send the list like this the new screen does not open and no error is shown. What is really weird, If I comment out the the part where I add the categories and just leave it as an empty list map the pushNamed action works and the screen opens. So I am wondering if it is how I am initializing the list map or if there is something else?
Thanks for your help