flutter 扑动:如果存在,检查局部图像

mm9b1k5b  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(102)

Following the last answer here, i am getting error
错误:无法将元素类型"Future"分配给列表类型"Widget"。

Future<Widget> getImage(String path) async {
  try {
    await rootBundle.load(path);
    return Image.asset(path);
  } catch (_) {
    return SizedBox.shrink();
  }
}

我的代码如下所示:

child: Column(
        children: <Widget>[
          Text('Text 1'),
          Text('Show below image if exist'),
          getImage('fileName'),
        ],

如何使用"getImage"返回所需的Widget?

eeq64g8w

eeq64g8w1#

试试Future Builder,即:

child: Column(
        children: <Widget>[
          Text('Text 1'),
          Text('Show below image if exist'),
          FutureBuilder(
              future: getImage("filename"),
              builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
                if (snapshot.connectionState == ConnectionState.done) {
                  return snapshot.data!;
                } else {
                  return SizedBox.shrink();
                }
              },
            ),
],

相关问题