I am trying to call deleteToDo()
and editToDo()
which both remove or edit an item in a To Do list. I am using the slidable flutter pub to format the To Do list. However this error is displaying ' This expression has a type of 'void' so its value can't be used.Also check type parameters and variables which might also be void.'
I have tried editing it to Future <Void>
but then get the following error also:
Any help is much appreciated.
my dart file:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:best_before/ShoppingList/toDoClass.dart';
import 'package:best_before/ShoppingList/toDos.dart';
import 'package:best_before/ShoppingList/Utils.dart';
class ToDoWidget extends StatelessWidget {
final ToDo toDo;
const ToDoWidget({
required this.toDo,
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) => ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Slidable(
key: Key(toDo.id),
startActionPane: ActionPane(
motion: ScrollMotion(),
dismissible: DismissiblePane(onDismissed: () {}),
children: [
SlidableAction(
onPressed: deleteToDo(context,toDo),
backgroundColor: Color(0xFFFE4A49),
foregroundColor: Colors.white,
icon: Icons.delete,
label: 'Delete',
),
],
),
endActionPane: ActionPane(
motion: ScrollMotion(),
children: [
SlidableAction(
flex: 2,
onPressed: editToDo(context, toDo),
backgroundColor: Colors.green,
foregroundColor: Colors.white,
icon: Icons.edit,
label:'Edit'
)
],
),
child:buildTodo(context),
),
);
Widget buildTodo(BuildContext context) => GestureDetector(
onTap: () => editToDo(context, toDo),
child: Container(
color: Colors.white,
padding: EdgeInsets.all(20),
child: Row(
children: [
Checkbox(
activeColor: Theme.of(context).primaryColor,
checkColor: Colors.white,
value: toDo.isDone,
onChanged: (_) {
final provider =
Provider.of<TodosProvider>(context, listen: false);
final isDone = provider.toggleTodoStatus(toDo);
Utils.showSnackBar(
context,
isDone ? 'Task completed' : 'Task marked incomplete',
);},
),
const SizedBox(width: 20),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
toDo.title,
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).primaryColor,
fontSize: 22,
),
),
if (toDo.description.isNotEmpty)
Container(
margin: EdgeInsets.only(top: 4),
child: Text(
toDo.description,
style: TextStyle(fontSize: 20, height: 1.5),
),
)
],
),
),
],
),
));
}
Void deleteToDo(BuildContext context, ToDo todo) {
final provider = Provider.of<TodosProvider>(context, listen: false);
provider.removeTodo(todo);
Utils.showSnackBar(context, 'Deleted the task');
}
Void editToDo(BuildContext context, ToDo todo) => Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => editToDoPage(Todo: todo),
),
);
CodePudding user response:
Try this:
SlidableAction(
onPressed: (context) => deleteToDo(context,toDo),
backgroundColor: Color(0xFFFE4A49),
foregroundColor: Colors.white,
icon: Icons.delete,
label: 'Delete',
)