所选文件的路径在 flutter 中抛出空检查错误

Path for picked file is throwing null check error in flutter

我有一张个人资料图片卡,正在 3 个不同的地方使用。我将一些参数传递给 ProfilePictureCard 以显示相机按钮。

例如,如果 ProfilePictureCard 在个人资料页面中使用,相机按钮将被隐藏,如果所述卡片在 EditProfilePage 中显示,相机按钮将可见。

这是 ProfilePictureCard 的代码:

class ProfilePictureCard extends StatefulWidget {
  final bool? isSetProfilePage;
  final bool? isEditable;
  File? imageFile;
  final String? imageUrl;
  ProfilePictureCard({
    Key? key,
    this.isSetProfilePage = false,
    this.isEditable = false,
    this.imageFile,
    this.imageUrl,
  }) : super(key: key);

  @override
  _ProfilePictureCardState createState() => _ProfilePictureCardState();
}

class _ProfilePictureCardState extends State<ProfilePictureCard> {
  @override
  void initState() {
    super.initState();
    print('Image Url: ${widget.imageUrl}');
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      width: 150,
      child: Stack(
        children: [
          Stack(
            children: [
              Center(
                child: _buildProfilePicture(),
              ),
              widget.isEditable == true
                  ? Positioned(
                      bottom: 0,
                      left: 55,
                      right: 55,
                      child: CameraButton(
                        onPressed: () {
                          _handleChangeProfilePicture();
                        },
                      ),
                    )
                  : Container(),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildProfilePicture() {
    return Container(
      height: 100,
      width: 100,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(10),
        border: Border.all(
          color: CustomColors.semiSecondary,
          width: 1,
        ),
      ),
      child: _buildImage(),
    );
  }

  Widget _buildImage() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10.0),
      child: widget.imageFile != null
          ? Image.file(
              widget.imageFile!,
              fit: BoxFit.cover,
            )
          : Image.network(
              widget.imageUrl ?? 'https://picsum.photos/200',
              fit: BoxFit.cover,
            ),
    );
  }

  void _handleChangeProfilePicture() async {
    final result = await showDialog(
      context: context,
      builder: (context) {
        return CameraDialog();
      },
    );
    print("Result: $result");
    if (result != null) {
      setState(() {
        widget.imageFile = result;
      });
      print(widget.imageFile);
    }
  }
}

_handleChangeProfilePicture 从画廊或相机拍摄一张照片,然后 returns 使用 Navigator.of(context).pop(finalImage);

在我的 EditProfilePage 中,我有一个 Update 按钮,它现在只打印出选定的图像路径。

EditProfilePage 文件如下所示:

class _EditProfilePage extends State<EditProfilePage {
    File? imageFile;

    @override
    Widget build(BuildContext context){
       ...other widgets
       ProfilePictureCard(
          isEditable: true, // because of this line the camera button is visible
          imageFile: imageFile,
          imageUrl: 'https://picsum.photos/id/237/200/300', // previous profile picture, will be replaced with selected image
       )
    }
    
    _handleUpdate(){
       print(imageFile.path);
    }
}

在 CameraDialog 内部,除了用于返回所选文件的基本 UI 东西外,我有这个:

  void _captureImage(context) async {
    final XFile? pickedFile =
        await _picker.pickImage(source: ImageSource.camera);
    if (pickedFile != null) {
      final File file = File(pickedFile.path);
      Navigator.of(context).pop(file);
    }
  }

但是当我按下更新时,我得到:Null check operator used on a null value 来自 print(imageFile.path);

出现这个问题的原因是什么,如何解决?

您应该使用函数 return 您的图像而不是传递对象,

class ProfilePictureCard extends StatefulWidget {
  final bool? isSetProfilePage;
  final bool? isEditable;
  final Function(File file) handleUpdate;
  final String? imageUrl;
  ProfilePictureCard({
    Key? key,
    this.isSetProfilePage = false,
    this.isEditable = false,
    this.handleUpdate,
    this.imageUrl,
  })

用法

if (result != null) {
    widget.handleUpdate(result);
}

正在编辑个人资料

 class _EditProfilePage extends State<EditProfilePage {
    File? imageFile;

    @override
    Widget build(BuildContext context){
       ...other widgets
       ProfilePictureCard(
          isEditable: true, // because of this line the camera button is visible
          handleUpdate: _handleUpdate,
          imageUrl: 'https://picsum.photos/id/237/200/300', // previous profile picture, will be replaced with selected image
       )
    }
    
    _handleUpdate(File file){
       print(file.path);
       setState(() {
         imageFile = file;
       });
    }
}