flutter 如何将FutureBuilder与FutureOr结合使用

n6lpvg4x  于 2022-11-17  发布在  Flutter
关注(0)|答案(1)|浏览(159)

我有一个变量FutureOr<Duration?>,我假设它可以返回Future<Duration?>Duration?,对吗?
如何像普通的future方法那样将它与FutureBuilder一起使用,如果Duration?是future方法,它将等待Duration?,如果不是,则直接使用Duration?值。
当我尝试以下操作时:

FutureBuilder<Duration?>(
            future: durationFuture,
            builder: (BuildContext context, snapshot) {
            /* my widgets implementations */
            },
          ),

这将抛出:

The argument type 'FutureOr<Duration?>?' can't be assigned to the parameter type 'Future<Duration?>?'

FutureOr类型是如何工作的,我如何基于它实现FutureBuider?

elcex8rz

elcex8rz1#

试试看:

FutureBuilder<Duration?>(
  future: Future.value(durationFuture),
  builder: (BuildContext context, snapshot) {
    /* my widgets implementations */
  },
)

相关问题