i get Error: The argument type 'Function?' can't be assigned to the parameter type 'void Function(bool?)?'. when i try to change toggle status of my checkbox how can i solve this problem?
itemCard
import 'package:flutter/material.dart';
class ItemCard extends StatelessWidget {
final String title;
final bool isDone;
final Function toggleStatus;//toggle status
const ItemCard(
{Key? key,
required this.title,
required this.isDone,
required this.toggleStatus})
: super(key: key);
@override
Widget build(BuildContext context) {
return Card(
elevation: 5,
shadowColor: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
child: ListTile(
title: Text(
title,
style: TextStyle(color: Colors.black),
),
trailing: Checkbox(
value: isDone,
activeColor: Colors.green,
onChanged: toggleStatus,//ERROR
),
));
}
}``
item
class Item {
final String title;
bool isDone;
Item({required this.title, this.isDone = false});
void toggleStatus() {
isDone = !isDone;
}
}
CodePudding user response:
Replace onChanged: toggleStatus,
with onChanged: (v) => toggleStatus(v)
.
Or final Function toggleStatus;
with final Function(bool?) toggleStatus;
CodePudding user response:
All function types have the base type Function
. This can include something as benign as void Function()
or as complex as Future<Map<String, dynamic>> Function(int, int, String, MyClass, List<int>)
. Your code is complaining because the onChanged
property of a Checkbox
must be void Function(bool)
, but you are passing in a value that is just Function
, which could be literally any kind of function.
You need to change the type of toggleStatus
in your class declaration from Function
to void Function(bool)
:
final void Function(bool) toggleStatus;
This means you need to change the signature of toggleStatus
in your item class as well:
void toggleStatus(bool status) {
isDone = status;
}