I have a page/screen CountryNameScreen this screen has list of countries like this.
var countries = [
'Australia',
'NewZeland',
'United State',
'Russia',
'United Kingdom',
'India',
];
There is an onTap function for the above countries. When we click on the country the detailed information page displayed on screen.
The problem is this: i want to navigate from other page of this app
to navigate to "United State" which is on index 2
onPressed: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: ((context) =>
const CountryNameScreen()),
),
);
},
What should i write while adding navigation code.
3
Answers
The problem is pretty poorly explained. Please try to add more details or explanation.
If you mean you want to dynamically go to a country page, try to add some constructor argument like this:
CountryNameScreen(countryName: indexNumber)
And with a variable (for ex: indexNumber) on top of the build method set the the indexNumber value to the value in the array.
So, you need to send an index so the screen be able to show you a country name
EX: For new york the index 2
Inside the CountryNameScreen, you need to add a property for this index
And then to add the property into class’ constructor
Then, from the place you want to navigate to this screen, you can do it by passing the variable like this:
In case that you want to show the countries in a list and to get the country dynamically, you can use the following code and you can edit the inner widget as you like
Hope it helps
Try below code I have try your Expected Result, when you click country name it jumps to selected country name page.
Your country List:
Your List Display Widget:
CountryNameScreen Widget:
Full Example
Country Name Result ->
Select Australia Country Result-
Select United State Country Result-