flutterdartmobx

How to fix "This expression has a type of 'void'..." error in Flutter?


I'm building a Pomodoro app in Flutter using MobX and encountering the error:

This expression has a type of void so its value can't be used.

Here's the relevant code and error screenshot:

Code:

class Pomodoro extends StatelessWidget {
  const Pomodoro({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final store = Provider.of<PomodoroStore>(context);

    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Expanded(child: Cronometro(),
          ),
          Padding(padding: EdgeInsets.symmetric(vertical: 40),
          child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            EntradaTempo(
                titulo: 'Work',
                valor: store.tempoWork,
                inc: store.incrementarTempoWork(),
                dec: store.reduceTempoRest(),
            ),
            EntradaTempo(
                titulo: 'Relax',
                valor: store.tempoRest,
                inc: store.incrementarTempoRest(),
                dec: store.reduceTempoRest(),
            ),
          ],
        ),
          ),
        ],
      ),
    );
  }
}

Error Screenshot:

enter image description here

As you can see in the image, I encountered errors in these lines:

EntradaTempo(
  titulo: 'Work',
  valor: store.tempoWork,
  inc: store.incrementarTempoWork(),
  dec: store.reduceTempoRest(),
),
EntradaTempo(
  titulo: 'Relax',
  valor: store.tempoRest,
  inc: store.incrementarTempoRest(),
  dec: store.reduceTempoRest(),
),

Specific Question: How can I resolve this void type error in my Flutter code within the context of my Pomodoro app?

Context: I'm following a tutorial, but the provided solution may not be directly applicable to my setup.


Solution

  • The parameters of EntradaTempo for inc and dec are probably some sort of Function, maybe a VoidCallback. The issue here is that you are not passing the function to the parameter, but you are calling the function and passing its return type, which might be void.

    You will probably want to remove the parentheses () at the end to avoid calling the function instead of simply passing it.

    EntradaTempo(
      titulo: 'Relax',
      valor: store.tempoRest,
      inc: store.incrementarTempoRest, // without '()'
      dec: store.reduceTempoRest, // without '()'
    ),
    

    An other way would be to create a new inline function there that calls it.

    EntradaTempo(
      titulo: 'Relax',
      valor: store.tempoRest,
      inc: () => store.incrementarTempoRest(),
      dec: () => store.reduceTempoRest(), // inline function, calling the passed function
    ),