提问人:Zulhisyam 提问时间:9/26/2023 更新时间:9/26/2023 访问量:34
Flutter:我不想将图像上传到服务器,但我想将其设置为可选,但出现错误“null check operator used on a null value”
Flutter: I don't want to uploading an image into server but i want to make it optional but an error appears "null check operator used on a null value"
问:
我将图像上传到服务器并且可以工作,但我想将其设置为可选,是否要上传取决于用户。但是,如果未选择任何图像,则会出现错误“null check operator used on a null value”。如果选择一张图片,它就可以工作了。那么如何解决这个问题,因为我已经在谷歌上搜索并按照建议更新了我的代码,但它们都没有奏效。
class _AduanPageState extends State<AduanPage> {
File? image;
Future pickImage(ImageSource source) async {
try {
final image = await ImagePicker().pickImage(source: source);
if (image == null) return;
final imageTemp = File(image!.path);
setState(() {
this.image = imageTemp;
});
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
@override
void initState() {
super.initState();
setState(() {
});
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: Visibility(
visible: _isvisible,
child: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[200],
border: Border.all(color: Colors.white),
borderRadius: BorderRadius.circular(12),
),
child: Column(
children: [
SizedBox(height: 10),
image != null
? Column(
children: [
Container(
child: Image.file(
image!,
width: 160,
height: 160,
fit: BoxFit.cover,
),
),
Container(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
fixedSize: Size(150, 40)),
onPressed: () {
setState(() {
image = null;
});
},
child: Icon(
Icons.delete_forever_outlined,
size: 35,
)),
),
SizedBox(height: 10),
],
)
:
SizedBox(height: 10),
Container(
width: MediaQuery.of(context).size.width,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(10.0)),
backgroundColor: kBiruGelap,
foregroundColor: Colors.white,
fixedSize: Size(150, 50)),
onPressed: () =>
pickImage(ImageSource.gallery),
child: Icon(
Icons.image_outlined,
size: 35,
),
),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
10.0)),
backgroundColor: kBiruGelap,
foregroundColor: Colors.white,
fixedSize: Size(150, 50)),
onPressed: () =>
pickImage(ImageSource.camera),
child: Icon(
Icons.camera_alt_outlined,
size: 35,
)
),
]),
),
SizedBox(height: 10),
],
),
),
)
],
),
),
),
SizedBox(height: 10),
ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0)),
backgroundColor: kBiruGelap,
padding: EdgeInsets.symmetric(horizontal: 131, vertical: 20)),
onPressed: () {
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Aduan Sedang Dihantar')),
);
aduanF2(
context,
_jenisAduanController.text,
_namaController.text,
_noICController.text,
_noTelController.text,
_emelController.text,
_keteranganController.text,
_kategoriRosakController.text,
_lokasiController.text,
image!,
_AccController.text,
keyUsername.toString());
}
},
child: Text(
'Hantar',
style: GoogleFonts.robotoCondensed(fontSize: 17),
),
),
],
),
),
);
}
}
}
这是调试控制台中出现的错误
════════ Exception caught by gesture ═══════════════════════════════════════════
Null check operator used on a null value
════════════════════════════════════════════════════════════════════════════════
那么,如何解决这个问题呢?提前感谢那些帮助的人。
答:
0赞
boby dhorajiya
9/26/2023
#1
你可以把它做成这样,它就可以工作了!
File image = File('');
而不是
File? image;
0赞
Nidhi Jain
9/26/2023
#2
发生这种情况是因为您在此处对图像(图像!
aduanF2(
context,
_jenisAduanController.text,
_namaController.text,
_noICController.text,
_noTelController.text,
_emelController.text,
_keteranganController.text,
_kategoriRosakController.text,
_lokasiController.text,
image!,
_AccController.text,
keyUsername.toString()
);
将其替换为 或 .image ?? false
image ?? File('')
希望它有所帮助。
评论
0赞
Zulhisyam
10/4/2023
按照建议添加后出现新错误。PathNotFoundException:无法检索文件的长度,路径 = '' (操作系统错误:没有这样的文件或目录,errno = 2) 对此有什么解决方案或建议吗?谢谢
0赞
Nidhi Jain
10/4/2023
这是因为 File(“”) 中有一个空字符串,您可以通过传递 false 代替 file 来尝试它,或者在 File 中添加一个虚拟文件 url。希望在此之后您不会遇到任何错误。
评论