Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: This expression has a type 'void' so its value can't be used

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.’
enter image description here

I have tried editing it to Future <Void> but then get the following error also:
Any help is much appreciated.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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),
        ),
      );

>Solution :

Try this:

SlidableAction(
   onPressed: (context) => deleteToDo(context,toDo),
   backgroundColor: Color(0xFFFE4A49),
   foregroundColor: Colors.white,
   icon: Icons.delete,
   label: 'Delete',
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading