Reputation: 3
This is how i am routing to next page ,
'/ot1': (context) => CustomListView(),
and it was working fine onTAP
onTap: (){
Navigator.pushNamed(context, '/ot1');
},
but when i created constructor in class CustomListView
and passed field i got this error that one argument is missing in this line '/ot1': (context) => CustomListView(),
this is the code of my class CustomListView
share below
class CustomListView extends StatelessWidget {
final List<Spacecraft> spacecrafts;
CustomListView(this.spacecrafts);
Widget build(context) {
return ListView.builder(
itemCount: spacecrafts.length,
itemBuilder: (context, int currentIndex) {
return createViewItem(spacecrafts[currentIndex], context);
},
);
}
I have searched for it for so much and didn't find a solution new to programming and FLUTTER language please HELP
Upvotes: 0
Views: 430
Reputation: 8010
If you want to pass data between screen using pushedName do it like,
Navigator.pushNamed(
context, '/ot1', arguments: mFeedData);
and fetch data like,
@override
Widget build(BuildContext context) {
mFeedData = ModalRoute.of(context).settings.arguments;
....
}
if you do not wish to pass any data then remove below portion from your code
CustomListView(this.spacecrafts)
or make it as an optional positional argument,
CustomListView([this.spacecrafts])
What is Positional parameter?
Wrapping a set of function parameters in [] marks them as optional positional parameters:
What is Named parameters?
When calling a function, you can specify named parameters using paramName: value. For example:
enableFlags(bold: true, hidden: false);
When defining a function, use {param1, param2, …}
to specify named parameters:
/// Sets the [bold] and [hidden] flags ...
void enableFlags({bool bold, bool hidden}) {...}
Upvotes: 2
Reputation: 627
Try adding square brackets [this.spacecrafts] so that the spacecraft argument becomes an optional argument.
class CustomListView extends StatelessWidget {
final List<Spacecraft> spacecrafts;
CustomListView([this.spacecrafts]); # <- this is where you use the brackets
...
Upvotes: 0
Reputation: 686
I am using this solution for navigation in my Flutter apps:
class RouteGenerator {
static Route<dynamic> generateRoute(RouteSettings settings) {
// Getting arguments passed in while calling Navigator.pushNamed
final args = settings.arguments;
switch (settings.name) {
case '/':
return MaterialPageRoute(builder: (_) => Home());
case '/routeWithoutArguments':
return MaterialPageRoute(
builder: (_) => PageWithoutArguments(),
);
case '/routeWithArguments':
if (args is String) { // I check if the arguments provided are valids
return MaterialPageRoute(
builder: (_) => PageWithArguments(myString: args),
);
}
return _errorRoute();
default:
return _errorRoute();
}
}
static Route<dynamic> _errorRoute() {
return MaterialPageRoute(builder: (_) {
return Scaffold(
appBar: AppBar(
title: Text('Error'),
),
body: Center(
child: Text('ERROR'),
),
);
});
}
}
Then in my widget App
:
MaterialApp(
initialRoute: '/',
onGenerateRoute: RouteGenerator.generateRoute,
...
)
Then to call my routes without arguments:
Navigator.pushNamed(
context,
'/routeWithoutArguments',
);
and to call with arguments:
Navigator.pushNamed(
context,
'/routeWithArguments',
arguments: "my argument",
);
I have learn this solution from ResoCoder
Upvotes: 0