flutterdartprovider

How to fix 'This expression has type void' error in Flutter?


I'm encountering the following error in my Flutter Pomodoro app:

lib/pages/pomodoro.dart:31:21: Error: This expression has type 'void' and can't be used.
                     ? null
                     ^

Code:

class Pomodoro extends StatelessWidget {
  // ... (rest of your widget code)

  @override
  Widget build(BuildContext context) {
    // ... (other parts of build method)

              Observer(
                builder: (_) => Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                     EntradaTempo(
                       titulo: 'Work',
                       valor: store.tempoWork,
                       inc: store.start && store.isWorking()
                           ? null // <-- Likely source of the error
                           : store.incrementarTempoWork(), 
                       dec: store.reduceTempoRest,
                     ),
                    // ...
                  ],
                ),
              ),
            // ...
  }
}

I suspect the error arises from the conditional expression within the inc property of EntradaTempo. How can I resolve this void type issue within a conditional expression?


Solution

  • In dart while checking conditions the value used to compare must not be null after upgrade to Null Safety. so you have to confirm it with ! symbol.

    ! symbol tells to compiler that particular field is non nullable ...

    may be below one work.

    inc: store!.start! && store!.isWorking()
       ? null
       : store!.incrementarTempoWork(),