Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 带图像文件的颤振载荷图像资源_Image_File_Flutter_Path_Assets - Fatal编程技术网

Image 带图像文件的颤振载荷图像资源

Image 带图像文件的颤振载荷图像资源,image,file,flutter,path,assets,Image,File,Flutter,Path,Assets,我有一个页面,允许用户发布一些东西,其中包括一个图像。当页面加载时,它会加载一个default.png图像,当用户单击它时,用户会被重定向到另一个页面,在那里他们可以拍照或从图库中进行选择 这里的主要问题是,当mypost-a-item页面加载时,它会查看Image.asset并查看其空值。因此,Image.asset能够加载defaultImage,但是当用户选择图像或拍照时,返回的值是一个文件,并且附加了路径,使其成为字符串(如下所示) 因此,Image.asset无法加载此文件.path

我有一个页面,允许用户发布一些东西,其中包括一个图像。当页面加载时,它会加载一个
default.png
图像,当用户单击它时,用户会被重定向到另一个页面,在那里他们可以拍照或从图库中进行选择

这里的主要问题是,当
mypost-a-item页面
加载时,它会查看
Image.asset
并查看其空值。因此,
Image.asset
能够加载
defaultImage
,但是当用户选择图像或拍照时,返回的值是一个
文件
,并且附加了
路径
,使其成为
字符串
(如下所示)

因此,
Image.asset
无法加载此
文件.path

这是我得到的错误:

Exception has occurred.
FlutterError (Unable to load asset: /data/user/0/com.flutter_project/cache/image_picker4920072427102948834.jpg)
我知道
Image.file
file.path
一起工作。。。但是我真的需要在某个地方做一个if语句来确保正确的图像显示吗

有没有更简单的方法

额外注意:当我在VisualStudio中进行刷新时(当我的应用程序运行时),图像实际上会加载(它应该加载)。。。很奇怪。。。 我的页面是有状态的,当我传递
结果(弹出页面中的文件)时,我对
imageName1执行
setstate

final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => PhotoPreviewPage()),
);

setState(() {
imageName1 = result;
});

你可以在下面这样做

File imageName1;
一旦你从弹出的路线中得到了图像,并且从导航器中得到了所选的图像结果

final result = await Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => PhotoPreviewPage()),
);

setState(() {
  imageName1 = File(result.path);
});
在构建方法中

(imageName1 == null)
    ? ClipOval(
       child: SizedBox(
       width: 120.0,
       height: 120.0,
       child: CachedNetworkImage(
         imageUrl: (widget.user.userImg != null) ? widget.user.userImg ?? '' : AssetImages.course,
         placeholder: (context, url) => Center(child: CircularProgressIndicator()),
         errorWidget: (context, url, error) => Image.asset(AssetImages.smallLoader),
         fit: BoxFit.fill,
        ),
      ),
     )
     : ClipOval(
         child: Image.file(
           imageName1,
           width: 120,
           height: 120,
           fit: BoxFit.cover,
         ),
       ),
非常感谢。“如果声明”成功了!
(imageName1 == null)
    ? ClipOval(
       child: SizedBox(
       width: 120.0,
       height: 120.0,
       child: CachedNetworkImage(
         imageUrl: (widget.user.userImg != null) ? widget.user.userImg ?? '' : AssetImages.course,
         placeholder: (context, url) => Center(child: CircularProgressIndicator()),
         errorWidget: (context, url, error) => Image.asset(AssetImages.smallLoader),
         fit: BoxFit.fill,
        ),
      ),
     )
     : ClipOval(
         child: Image.file(
           imageName1,
           width: 120,
           height: 120,
           fit: BoxFit.cover,
         ),
       ),