Flutter 如果不是',则显示默认图像;在颤振中由图像选择器拾取的t
我有一个图像选择器函数,它从gallery中拾取图像,然后将其分配给\u image变量,该变量是字符串。它将其转换为base64,因为这是必需的。我想知道如果没有拾取图像(拾取的图像为空),如何从资源中获取默认图像作为_图像。下面是我试过注释的代码和内容,它位于代码中的else下:Flutter 如果不是',则显示默认图像;在颤振中由图像选择器拾取的t,flutter,dart,Flutter,Dart,我有一个图像选择器函数,它从gallery中拾取图像,然后将其分配给\u image变量,该变量是字符串。它将其转换为base64,因为这是必需的。我想知道如果没有拾取图像(拾取的图像为空),如何从资源中获取默认图像作为_图像。下面是我试过注释的代码和内容,它位于代码中的else下: Future _getImage() async { PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery); se
Future _getImage() async {
PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
final file = File(pickedFile.path);
_image = Utility.base64String(file.readAsBytesSync());
} else {
//if image wasn't picked, get the default one from assets
print('No image selected.');
// final file = File(AssetImage('assets/defaultfood.jpg').toString());
// _image = Utility.base64String(file.readAsBytesSync());
//final file = File('assets/defaultfood.jpg');
//_image = Utility.base64String(file.readAsBytesSync());
}
});
}添加像这样的三元条件的图像占位符
child: pickedFile == null ? Image.asset("assets/images/man_user.png",height: 100, width: 100): Image.file(pickedFile, height: 100, width: 100),
像这样添加图像占位符
child: pickedFile == null ? Image.asset("assets/images/man_user.png",height: 100, width: 100): Image.file(pickedFile, height: 100, width: 100),
你说得对,事实上我自己也弄明白了,我有几分钟的时间,就是弄不明白。无论如何谢谢你!你说得对,事实上我自己也弄明白了,我有几分钟的时间,就是弄不明白。无论如何谢谢你!