【问题标题】:Flutter/Dart: Image widget and check if file existFlutter/Dart:图像小部件并检查文件是否存在
【发布时间】:2021-08-23 03:18:18
【问题描述】:

我有一个布局,它仅在图像小部件存在时才放置它。 但显示错误为“无法加载资产:资产/无”

代码正确吗?

  //  list of filename ['index.jpg','','index.jpg','none']
  Widget displayTextImage(String question, String filename) {
  String assetFolder = 'assets/';
  print(filename);
  return (Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: [
      Text(
        question,
      ),
      io.File(assetFolder + filename).exists() != null
          ? Image.asset(assetFolder + filename)
          : SizedBox(height: 1),
    ],
  ));
}

【问题讨论】:

    标签: image flutter user-interface dart


    【解决方案1】:

    您可以使用此代码:

    import 'package:flutter/services.dart' show rootBundle;
    
    ByteData bytes;
    
    try{
       bytes = await rootBundle.load('assets/placeholder.png');
       asset file exist and create your ui code...
    }catch(e){
       asset file not exist...
    }
    

    在这篇文章中阅读有关此主题的更多信息:How to read bytes of a local image file in Dart/Flutter?

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 2018-11-18
      • 2021-02-11
      • 2021-02-15
      • 1970-01-01
      • 2018-12-09
      • 2017-05-11
      • 2011-01-12
      • 2016-01-27
      相关资源
      最近更新 更多