如何修复 Flutter 中的“This expression has a type of 'void'...”错误?

问题描述 投票:0回答:2

我正在使用 MobX 在 Flutter 中构建番茄钟应用程序,并遇到错误“此表达式的类型为‘void’,因此无法使用其值。”以下是相关代码和错误截图:

代码:

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(),
            ),
          ],
        ),
          ),
        ],
      ),
    );
  }
}

错误截图:

正如您在图片中看到的,我在这些行中遇到了错误:

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

具体问题: 如何在 Pomodoro 应用程序的上下文中解决 Flutter 代码中的“void”类型错误?

上下文:我正在遵循教程,但提供的解决方案可能无法直接适用于我的设置。

flutter dart mobx
2个回答
1
投票

EntradaTempo

inc
dec
参数可能是某种
Function
,也许是
VoidCallback
。这里的问题是,您没有将函数传递给参数,而是调用该函数并传递其返回类型,这可能是 
void

您可能需要删除末尾的括号

()

 以避免调用该函数而不是简单地传递它。

EntradaTempo( titulo: 'Relax', valor: store.tempoRest, inc: store.incrementarTempoRest, // without '()' dec: store.reduceTempoRest, // without '()' ),
另一种方法是创建一个新的内联函数来调用它。

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

1
投票
调用不带括号的函数

()

:

EntradaTempo( titulo: 'title', valor: store.tempoRest, inc: store.incrementarTempoRest, dec: store.reduceTempoRest, ),
    
© www.soinside.com 2019 - 2024. All rights reserved.