0

Following the last answer here, i am getting error

error: The element type 'Future' can't be assigned to the list type 'Widget'.

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

My code looks like this:

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

How to use 'getImage' to return the desired Widget?

1 Answers1

0

Try Future Builder, i.e :


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