【发布时间】:2021-10-11 18:52:52
【问题描述】:
这是我的背景图片:
Container(
decoration: BoxDecoration(image: DecorationImage(fit: BoxFit.cover, image: FileImage(widget.lightAnimation.imageFile))),
widget.lightAnimation.imageFile: FutureBuilder(
future: getImageFile(),
builder: (context, snapshot) => snapshot.data,
)
Future<File> getImageFile() 返回一个File;
问题是输出必须是一个小部件,File 和 BoxDecoration 都不被认为是 Widget 下一个最接近的将是 Container,但是这在我的情况下不起作用因为 Container 的子节点非常健壮,包含许多变量、函数等。
换句话说,我可以做的是:
FutureBuilder(
future: getImageFile(),
builder: (context, snapshot) => Container(
decoration: BoxDecoration(image: DecorationImage(fit: BoxFit.cover, image: FileImage(snapshot.data))),
),
)
但是,在这种情况下,我必须将我所有的大孩子都搬到 FutureBuilder 中,因为 Container 会随身携带,我不想这样做。
我可以使用任何替代方法来代替不需要 Widget 作为输出的 FutureBuilder 吗?或者也许有其他可能的方法来让我的背景图像具有未来的“建造者/制造者/功能”?
【问题讨论】:
-
你试过 getImageFile().whenComplete(() => ...) 还是 .then()
-
我不确定你会在哪里添加它。能具体说明一下吗?
-
我不明白为什么
FutureBuilder如果你有一些Future没有用:你有一些Container那你为什么不想从builder: ....代码返回它? -
因为让 FutureBuilder 返回一个
File、FileImage、DecoratingImage给我一个错误,即 FutureBuilder 构建器必须构建/返回一个小部件,例如Container。因此,与其在 FutureBuilder 中返回/构建它:builder: (context, snapshot) => snapshot.data,我将不得不返回/构建:builder: (context, snapshot) => Container(decoration: BoxDecoration(image: DecorationImage(fit: BoxFit.cover, image: FileImage(snapshot.data))),,我不想这样做。 -
如果你不想
FutureBuilder使用getImageFile().then((file) => setState(() => _file = file));并将其添加到initState()方法中 - 但老实说这是FutureBuilder完全构建你的小部件的工作,上面的代码你正在复制FutureBu8ilder的代码
标签: flutter dart asynchronous future