如何解决Flutter开发中返回null的报错?投诉应用ImagePicker功能异常修复
问题原因及修复方案
错误触发的3个核心原因
_decideImageView函数声明返回值为非空Widget类型,但else分支仅创建了Image组件未添加return关键字,函数走else分支时会返回null,不符合非空返回要求
- 用
late修饰imageFile的同时判断它是否为null:late关键字声明的变量默认不允许为null,你需要将其改为可空类型File? imageFile才能做空判断
- 用
ImagePicker().pickImage返回的是XFile类型,直接强转为io.File会抛出类型转换异常,需要通过XFile.path构造File对象,同时要处理用户取消选择时返回null的情况
修正后的完整代码
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; class ComplaintNo1 extends StatefulWidget { @override _ComplaintNo1State createState() => _ComplaintNo1State(); } class _ComplaintNo1State extends State<ComplaintNo1> { // 修改为可空类型,允许为空 File? imageFile; _openGallery(BuildContext context) async{ final XFile? picture = await ImagePicker().pickImage(source: ImageSource.gallery); // 处理用户未选择图片的空情况 if(picture != null) { setState(() { // 用XFile的path构造File对象 imageFile = File(picture.path); }); } Navigator.of(context).pop(); } _openCamera(BuildContext context) async{ final XFile? picture = await ImagePicker().pickImage(source: ImageSource.camera); if(picture != null) { setState(() { imageFile = File(picture.path); }); } Navigator.of(context).pop(); } Future<void> _showChoiceDialog(BuildContext context) { return showDialog(context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('Make a choice!'), content: SingleChildScrollView( child: ListBody( children: <Widget>[ GestureDetector( child: const Text('Gallery'), onTap: () { _openGallery(context); }, ), const Padding(padding: EdgeInsets.all(8.0)), GestureDetector( child: const Text('Camera'), onTap: () { _openCamera(context); }, ) ], ), ), ); }); } Widget _decideImageView() { if(imageFile == null) { return const Text('No Image Selected!'); } else { // 补全return关键字 return Image.file(imageFile!, width: 100, height: 200); } } @override Widget build(BuildContext context) { return Scaffold( body: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Icon( Icons.camera_alt, ), ElevatedButton(onPressed: () { _showChoiceDialog(context); }, child: const Text('Select Image'), ), _decideImageView(), const SizedBox( width: 20.0, height: 20.0, ), TextFormField( decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Title of complaint', prefixIcon: Icon(Icons.title), labelStyle: TextStyle( fontSize: 20.0, color: Colors.blueAccent, ), ), ), const SizedBox( width: 20.0, height: 20.0, ), TextFormField( decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Time', prefixIcon: Icon(Icons.access_time), labelStyle: TextStyle( fontSize: 20.0, color: Colors.blueAccent, ), ), ), const SizedBox( width: 20.0, height: 20.0, ), TextFormField( decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Starting date', prefixIcon: Icon(Icons.date_range), labelStyle: TextStyle( fontSize: 20.0, color: Colors.blueAccent, ), ), ), const SizedBox( width: 20.0, height: 20.0, ), TextFormField( decoration: const InputDecoration( border: OutlineInputBorder(), labelText: 'Description of complaint', prefixIcon: Icon(Icons.description), labelStyle: TextStyle( fontSize: 20.0, color: Colors.blueAccent, ), ), maxLines: 5, ), ], ), ); } }
内容的提问来源于stack exchange,提问作者suk
相关产品推荐
相关产品推荐

