【问题标题】:Image received from API, showing blank image从 API 接收到的图像,显​​示空白图像
【发布时间】:2021-06-22 12:45:44
【问题描述】:

当我尝试从 API 获取图像时,它以列表的形式出现。为了转换为A类型的List。我收到一张空白图片,并收到有关此异常的通知。如何使图像显示而不是获得空白图像

════════图片资源服务捕获的异常════════════════════════════ 无法打开文件,路径 = 'https:.......amazonaws.com/Mealbox/e5585d92-fd5c-4e62-b17c-84f11127024d/image_picker4761428725156146366.jpg'(操作系统错误:没有这样的文件或目录,errno = 2) ══════════════════════════════════════════════════ ══════════════════════════════

这是我的代码。

List<File> images = List<File>();
void sendDataForFourthPage(List<String> imagesIn) {
  images = imagesIn.map((e) => File(e)).toList();
}

FourthPage 用于从图库中获取图像 这是第四页的代码

class FourthPage extends StatefulWidget {
  @override
  _FourthPageState createState() => _FourthPageState();
}

class _FourthPageState extends State<FourthPage> {
  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
  }

  // save the result of gallery fileUserOptions
  File galleryFile;

  @override
  Widget build(BuildContext context) {
    //display image selected from gallery
    imageSelectorGallery() async {
      galleryFile = await ImagePicker.pickImage(
          source: ImageSource.gallery, imageQuality: 20);
      if (images.length < 5) {
        images.add(galleryFile);
      } else {
        images.removeLast();
        images.add(galleryFile);
      }
      print("You selected gallery image : " + galleryFile.path);
      setState(() {});
    }

    return new SingleChildScrollView(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(bottom: 7),
            child: Text(
              "Upload Picture",
              style: TextStyle(fontFamily: "Montserrat Medium", fontSize: 20),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 20),
            child: Text(
              "Lorem ipsum dolor sit amet, consectetur",
              style: TextStyle(
                  fontFamily: "Montserrat Regular",
                  color: sankaraGreyColor,
                  fontSize: 12),
            ),
          ),
          new RaisedButton(
            child: new Text('+'),
            onPressed: imageSelectorGallery,
          ),
          SizedBox(height: 20),
          new Container(
            height: 2000,
            child: GridView.count(
              crossAxisSpacing: 6,
              mainAxisSpacing: 6,
              crossAxisCount: 3,
              children: List.generate(images.length, (index) {
                return Column(children: <Widget>[
                  Container(
                      height: 90,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: ClipRRect(
                        child: Image.file(images[index], fit: BoxFit.cover),
                        borderRadius: BorderRadius.circular(10),
                      )),
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        images.removeAt(index);
                      });
                    },
                    child: Padding(
                      padding: const EdgeInsets.all(0.0),
                      child: Align(
                        alignment: Alignment.bottomRight,
                        child:
                            Icon(Icons.cancel, color: Colors.black, size: 20),
                        widthFactor: 4.3,
                        heightFactor: 0.5,
                      ),
                    ),
                  ),
                ]);
              }),
            ),
          )
        ],
      ),
    );
  }

  Widget displaySelectedFile(File file) {
    return new SizedBox(
      height: 200.0,
      width: 300.0,
      //child: new Card(child: new Text(''+galleryFile.toString())),
      //child: new Image.file(galleryFile),
      child: file == null
          ? new Text('Sorry nothing selected!!')
          : new Image.file(file),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    从错误消息看来,您正在将 URL 传递给 Image.file。这是行不通的,因为Image.file 需要来自您的文件系统的文件。 请改用Image.network

    【讨论】:

    • 是的,主要功能是能够从手机(文件系统)上传图像。但我也希望能够检索已经上传到 API 的先前图像并能够操作它们,即(删除已经完成的添加)。但我不知道如何构建我的代码以添加从已存储图像的 API 中提取的功能
    猜你喜欢
    • 2014-02-10
    • 1970-01-01
    • 2014-07-17
    • 2019-04-04
    • 2013-11-12
    • 1970-01-01
    • 2011-09-09
    • 2014-11-27
    • 2015-11-18
    相关资源
    最近更新 更多