I'm trying to implement a checkbox widget that ought to only allow me to pass data to a new screen once I have selected a specific checkbox . whilst i was trying to pass these different values to a new screen i keep on getting the error :
The named parameter 'saveCheck' is required, but there's no corresponding argument. Try adding the required argument.
below is the snippet of code that displays the checkbox ui:
class CheckBoxPage extends ConsumerStatefulWidget {
const CheckBoxPage({Key? key}) : super(key: key);
static const String route = "/checkbox";
@override
ConsumerState<CheckBoxPage> createState() => _CheckBoxPageState();
}
class _CheckBoxPageState extends ConsumerState<CheckBoxPage> {
@override
Widget build(BuildContext context) {
final readAsync = ref.watch(checkRepoProvider);
final model = ref.watch(checkboxProvider);
bool check1 = false;
bool check2 = false;
String? saveFirstValue;
String? saveSecondValue;
return Scaffold(
appBar: AppBar(
title: const Text("check"),
),
bottomNavigationBar: Padding(
padding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
child: MaterialButton(
padding: const EdgeInsets.all(16),
color: Colors.black,
onPressed: () {
if (saveFirstValue!.isNotEmpty && saveSecondValue!.isNotEmpty) {
String bothValues = "$saveFirstValue $saveSecondValue";
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
DisplayPage(bothValues: bothValues)));
} else {
if (saveFirstValue!.isNotEmpty) {
String saveCheck = "$saveFirstValue";
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPage(
saveCheck: saveCheck,
)));
} else if (saveSecondValue!.isNotEmpty) {
String saveCheck2 = "$saveSecondValue";
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPage(
saveCheck2: saveCheck2,
)));
}
}
},
child: const Text(
"Add meal",
style: TextStyle(color: Color.fromARGB(255, 247, 245, 245)),
),
),
),
body: readAsync.when(
data: (check) => ListView(
children: check
.map(
(e) => Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CheckboxListTile(
title: Text(e.checkbox1),
controlAffinity: ListTileControlAffinity.leading,
value: check1,
onChanged: (value) {
check1 = value!;
saveFirstValue = e.checkbox1;
},
contentPadding: EdgeInsets.zero,
),
CheckboxListTile(
title: Text(e.checkbox2),
controlAffinity: ListTileControlAffinity.leading,
value: check2,
onChanged: (value) {
check2 = value!;
saveSecondValue = e.checkbox2;
},
contentPadding: EdgeInsets.zero,
),
],
),
)
.toList()),
error: (e, s) => Center(
child: Text("$e"),
),
}
below is the screen where i'm trying to display selected values:
class DisplayPage extends StatefulWidget {
const DisplayPage( {Key? key,
required this.saveCheck, required this.saveCheck2, required this.bothValues,
}) : super(key: key);
final String saveCheck;
final String saveCheck2;
final String bothValues;
static const String route = "/display";
@override
State<DisplayPage> createState() => _DisplayPageState();
}
class _DisplayPageState extends State<DisplayPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("display"),
),
body: Column(
children: [Text(widget.saveCheck)],
),
);
}}
CodePudding user response:
Named constructor are optional by default, while we use final variable we need to make it required or need to provide default value.
Making nullable keep both optional and not required
const DisplayPage({
Key? key,
this.saveCheck,
this.saveCheck2,
this.bothValues,
}) : super(key: key);
final String? saveCheck;
final String? saveCheck2;
final String? bothValues;
Also you can provide default value
const DisplayPage({
Key? key,
this.saveCheck = "default",
this.saveCheck2= "default",
this.bothValues= "default",
}) : super(key: key);
final String saveCheck;
final String saveCheck2;
final String bothValues;
static const String route = "/display";
You can find more about understanding-null-safety and using-constructors
CodePudding user response:
You've defined your DisplayCheck widget so that you have to pass all three parameters (savecheck, savecheck2, bothValues) when you instantiate it. In your onPressed callback, you are trying to
A. instantiate it only passing bothValues
B. instantiate it only passing saveCheck
C. instantiate it only passing saveCheck2
All of those are illegal by your definition. You have to either pass all required constructor arguments, or make the constructor arguments in your DisplayCheck widget non-required.
By the way, your IDE of choice should be telling you about this. If it is not, I suggest you review your IDE setup.