【问题标题】:Error: This expression has type 'void' and can't be used. ? null (Flutter)错误:此表达式的类型为 \'void\',无法使用。 ?空(颤振)
【发布时间】:2022-09-23 05:51:19
【问题描述】:

我在终端上收到此错误

lib/pages/pomodoro.dart:31:21:错误:此表达式的类型为 \'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: Observer(
            builder: (_) => Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              EntradaTempo(
                titulo: \'Work\',
                valor: store.tempoWork,
                inc: store.start && store.isWorking()
                    ? null
                : store.incrementarTempoWork(),
                dec: store.reduceTempoRest,
              ),
              EntradaTempo(
                titulo: \'Relax\',
                valor: store.tempoRest,
                inc: store.incrementarTempoRest,
                dec: store.reduceTempoRest,
              ),
            ],
          ),
          )
          ),
        ],
      ),
    );
  }
}

如何解决这个问题?

先感谢您

    标签: flutter


    【解决方案1】:

    这可能是因为store.startstore.incrementarTempoWork() 没有返回值,这是void

    还要检查inc 是否接受null

    inc: store.start && store.isWorking()
       ? null
       : store.incrementarTempoWork(),  // <<<< probably this one is void
    

    【讨论】:

      【解决方案2】:

      在检查条件时,用于比较的值不能为空 升级到之后零安全.所以你必须用 !象征。

      符号告诉编译器特定字段不可为空...

      可能低于一项工作。

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

      【讨论】:

        猜你喜欢
        • 2023-02-02
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 2014-11-05
        • 2019-11-12
        相关资源
        最近更新 更多