【发布时间】: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),
);
}
}
【问题讨论】: