title: How to check if image assets exist then use it as circle avatar or write first letter of name in circle avatar
body: <p>i have some images in my assets folder. I just want to check whether the user entered name is matching with image or not. if it is matching then I want to use that image in circle avatar or else use the first letter of the name in circle avatar.</p>
  <p><strong>My Logic</strong></p>
  <p>I am trying to load image via <code>rootBundle.loadString()</code>. If the image or path does not exist then it will throw an exception. that's how I know that I need to use the first letter of the name.</p>
  <p><strong>What I have tried so far</strong></p>
  <p>My first approach was just to check whether the path exists or not. for that, I have used the following instruction. but It was giving me always false. I could not find a proper reason for that.</p>
  <pre><code>io.File(syncPath).exists();
  </code></pre>
  <p>therefore, I am using <code>rootBundle.loadString()</code> to catch exception.</p>
  <p>I have searched on the internet but I could not my found a solution related to my problem (probably because of my bad searching skill)</p>
  <p>I have also tried following solutions,
  I have tried wrapping <code>FutureBuilder</code> on <code>Card()</code> like</p>
  <pre><code>  child: FutureBuilder&lt;String&gt;(
      future: rootBundle.loadString(imagePath),
      builder: (BuildContext context,snapshot) {
             if (snapshot.hasData){
             return Card(...)// card and avatar with image
           } else {
           return Card(...)// card and avatar with first letter of name
          }
  }
  </code></pre>
  <p>but still, I can not grab exception.</p>
  <p>here is my code.</p>
  <pre><code>class ItemCardListTile extends StatefulWidget {
    final bool tobuy;
    final String name;
    const ItemCardListTile({
      Key? key,
      required this.tobuy,
      required this.name,
    }) : super(key: key);
  
    @override
    _ItemCardListTileState createState() =&gt; _ItemCardListTileState();
  }
  
  class _ItemCardListTileState extends State&lt;ItemCardListTile&gt; {
    final log = logger(ItemCardListTile);
  
    String assetsPath = 'assets/images/';
  
    String imageExt = '.png';
  
    @override
    void initState() {
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      final String firstLettter = widget.name[0];
      AssetImage? imageAssets;
      String imagePath = '$assetsPath${widget.name.toLowerCase()}$imageExt';
  
      Future&lt;AssetImage?&gt; checkImageExist(String imagePath) async {
        try {
          await rootBundle.loadString(imagePath);
          imageAssets = AssetImage(imagePath);
        } catch (e) {
          print(e.toString());
          return null;
        }
      }
  
      return Card(
        child: widget.tobuy
            ? ListTile(
                leading: (checkImageExist(imagePath) == null)
                    ? CircleAvatar(
                        // backgroundImage: imageAssets,
                        child: Text(firstLettter),
                      )
                    : CircleAvatar(backgroundImage: imageAssets),
                title: Text(widget.name),
              )
            : const SizedBox(),
      );
    }
  }
  </code></pre>
  <p>Please help me to solve my issue.
  thank you very much in advance.</p>
  <p>updated my code as per the comment of @PeterKoltai
  Now I am getting  <code>FormatException: Unexpected extension byte (at offset 0)</code> . my image path is correct. I can display a circle avatar image using AssestImage().</p>
  <pre><code> Widget build(BuildContext context) {
      final String firstLettter = widget.name[0];
      final String _imagePath = 'assets/images/broccoli.png';
  
      Future checkImageExist(String imagePath) async {
        try {
          return await rootBundle.loadString(imagePath);
        } catch (e) {
          print(e.toString());
          return null;
        }
      }
  
      return FutureBuilder(
        future: checkImageExist(_imagePath),
        builder: (BuildContext context, snapshot) {
          if (snapshot.hasError != null) {
            return Card(
              child: widget.tobuy
                  ? ListTile(
                      leading:CircleAvatar(
                        child: Text(firstLettter),
                      ),
                      title: Text(widget.name),
                    )
                  : const SizedBox(),
            );
          } else
            return Container();
        },
      );
    }
  </code></pre>