The named parameter 'name' isn't defined. Try correcting the name to an existing named parameter's name, or defining a named parameter with the name 'name'.
import 'package:flutter/material.dart';
import 'package:navigation/screen2.dart';
class Screenone extends StatelessWidget {
const Screenone({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
// navigation to next page.
/*body:
Column(children: [
Text("Screen one"),
ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (ctx) {
return Screentwo();
},
),
);
},
child: Text("Go to next page"),
)
]),*/
body: SafeArea(
child: ListView.separated(
itemBuilder: (context, index) {
return ListTile(
title: Text('person $index'),
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return Screentwo(name: 'person $index');
},
),
);
},
);
},
separatorBuilder: ((context, index) {
return Divider();
}),
itemCount: 20),
),
);
} }
CodePudding user response:
To be able to pass a parameter to from X page to Screentwo you must ask for a parameter from Screentwo in the first place;
class Screentwo extends StatelessWidget {
final String name;
const Screentwo({Key? key, required this.name}) : super(key: key);
@override
Widget build(BuildContext context) {
//return your widget
}
}
CodePudding user response:
Pass the optional name parameter in ScreenTwo.
class ScreenTwo extends StatelessWidget {
final String? name;
const ScreenTwo({Key? key, this.name}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text(name ?? ""),
);
}
}
Call Example:
return Screentwo(name: 'person name');
or
return Screentwo();