Issue
I've changed my Navigator.push to Navigator.pushNamed but I don't understand how to pass data and receive/use it. If we use Navigator.push, we can just do this and call the constructor to use the data, but if we use Navigator.pushNamed, it sends arguments.
Now, how to call/use these arguments?
in /surveyPage page how do i call the arguments? in /surveyPage i have constructor languageCode but since i use pushNamed i cannot put languageCode.
this is the widget that i want to recieve the argument, i want to pass the argument to languageCode
class SurveyConsentPage extends StatefulWidget {
SurveyConsentPage({this.languageCode, Key key}) : super(key: key);
final String languageCode;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SurveyConsentPage(
languageCode: 'id',
),
),
);
pushNamed
Navigator.of(context).pushNamed('/surveyPage', arguments: 'id');
routes
case '/surveyPage':
return MaterialPageRoute(builder: (context) => SurveyConsentPage());
break;
Solution
In your route case:
case '/surveyPage':
final code = settings.arguments as String;
return MaterialPageRoute(builder: (context) => SurveyConsentPage(languageCode: code));
break;
Make sure you have defined String languageCode as a required parameter to your SurveyConsentPage widget.
class SurveyConsentPage extends StatefulWidget {
final String languageCode;
SurveyConsentPage({required this.languageCode, Key key}) : super(key: key);
Answered By - Roslan Amir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.