错误:无法将参数类型“File?”赋给参数类型“File.”Flutter

jgovgodb  于 2022-12-05  发布在  Flutter
关注(0)|答案(2)|浏览(137)

我想从图库中选择一个图像,并在使用image_picker单击容器时将其显示在容器底部。但是**'参数类型“File?”无法分配给参数类型“File”。'**出现错误。我在Google和StackOverFlow上努力搜索答案,但无法解决。我该怎么办?
这是我的代码:

import 'dart:io';

import 'package:flutter/material.dart';
import '../../../constants.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';

class UploadFishingVesselImages extends StatefulWidget {
  const UploadFishingVesselImages({Key? key}) : super(key: key);

  @override
  State<UploadFishingVesselImages> createState() =>
      _UploadFishingVesselImagesState();
}

class _UploadFishingVesselImagesState extends State<UploadFishingVesselImages> {
  File? _image;
  final _picker = ImagePicker();

  Future choiceImage() async {
    var pickedImage = await _picker.pickImage(source: ImageSource.gallery);
    if (pickedImage == null) return null;
    setState(() {
      _image = File(pickedImage.path);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: baseColor10,
        elevation: 0,
        leading: IconButton(
          onPressed: () {
            Get.back();
          },
          icon: Icon(Icons.arrow_back),
          color: baseColor50,
        ),
        title: Text(
          '선박판매등록',
          style: TextStyle(
            color: baseColor50,
            fontFamily: 'semi-bold',
            fontSize: titleMedium,
          ),
        ),
      ),
      body: GestureDetector(
        onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
        child: SingleChildScrollView(
          child: SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  TopBar(context),
                  marginHeight32,
                  buildTextColumn(),
                  Row(
                    children: [
                      GestureDetector(
                        onTap: () async {
                          await choiceImage();
                        },
                        child: Expanded(
                          child: Container(
                            height: 90,
                            width: MediaQuery.of(context).size.width * 0.3,
                            decoration: BoxDecoration(
                              color: baseColor20,
                            ),
                          ),
                        ),
                      ),
                      SizedBox(width: 10),
                      Expanded(
                        child: Container(
                          height: 90,
                          width: MediaQuery.of(context).size.width * 0.3,
                          decoration: BoxDecoration(
                            color: baseColor20,
                          ),
                        ),
                      ),
                      SizedBox(width: 10),
                      Expanded(
                        child: Container(
                          height: 90,
                          width: MediaQuery.of(context).size.width * 0.3,
                          decoration: BoxDecoration(
                            color: baseColor20,
                          ),
                        ),
                      ),
                    ],
                  ),
                  // Error occurs. 'The argument type 'File?' can't be assigned
                  // to the parameter type 'File'.
                  Image.file(_image),
                ],
              ),
            ),
          ),
        ),
      ),
      floatingActionButton: Visibility(
        visible: MediaQuery.of(context).viewInsets.bottom == 0,
        child: FloatingActionButton(
          backgroundColor: primaryColor50,
          foregroundColor: baseColor10,
          focusColor: primaryColor50,
          onPressed: () {},
          child: Icon(
            Icons.arrow_forward_ios_rounded,
          ),
        ),
      ),
    );
  }

// 상단 텍스트
  Column buildTextColumn() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '사진 등록',
          style: TextStyle(
            color: baseColor50,
            fontSize: titleMedium,
            fontFamily: 'semi-bold',
          ),
        ),
        marginHeight4,
        Text(
          '판매선박의 사진을 등록해주세요.(최대 20장)'
          '\n다양하고 선명한 사진을 올릴 경우, 판매 확률이 더욱 높아집니다.',
          style: TextStyle(
            fontSize: bodySmall,
          ),
        ),
        marginHeight32,
      ],
    );
  }

// 상단바
  Column TopBar(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [
            Expanded(
              child: Container(
                height: 3,
                color: baseColor30,
                width: MediaQuery.of(context).size.width * 0.2,
              ),
            ),
            Expanded(
              child: Container(
                height: 3,
                color: baseColor30,
                width: MediaQuery.of(context).size.width * 0.2,
              ),
            ),
            Expanded(
              child: Container(
                height: 3,
                color: baseColor30,
                width: MediaQuery.of(context).size.width * 0.2,
              ),
            ),
            Expanded(
              child: Container(
                height: 3,
                color: primaryColor50,
                width: MediaQuery.of(context).size.width * 0.2,
              ),
            ),
            Expanded(
              child: Container(
                height: 3,
                color: baseColor30,
                width: MediaQuery.of(context).size.width * 0.2,
              ),
            ),
          ],
        ),
        marginHeight4,
        Row(
          children: [
            Expanded(
              child: Container(
                child: Center(
                  child: Text(
                    '선박정보',
                    style: TextStyle(
                      fontFamily: 'medium',
                      fontSize: labelSmall,
                      color: baseColor30,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                child: Center(
                  child: Text(
                    '옵션',
                    style: TextStyle(
                      fontFamily: 'medium',
                      fontSize: labelSmall,
                      color: baseColor30,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                child: Center(
                  child: Text(
                    '설명등록',
                    style: TextStyle(
                      fontFamily: 'medium',
                      fontSize: labelSmall,
                      color: baseColor30,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                child: Center(
                  child: Text(
                    '사진등록',
                    style: TextStyle(
                      fontFamily: 'medium',
                      fontSize: labelSmall,
                      color: primaryColor50,
                    ),
                  ),
                ),
              ),
            ),
            Expanded(
              child: Container(
                child: Center(
                  child: Text(
                    '결제',
                    style: TextStyle(
                      fontFamily: 'medium',
                      fontSize: labelSmall,
                      color: baseColor30,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }
}
kokeuurv

kokeuurv1#

您正面临这个错误,原因是Image.file()不接受可为空的变量,而your _image变量是可为空的,因此您必须为它设置条件,您可以这样设置。

_image==null?Container(): Image.file(_image??File('')),

这个条件不会抛出任何可为空错误。2而且如果你想在_image为空时显示其他内容,你可以放置任何不同的部件而不是容器。

vsikbqxv

vsikbqxv2#

如果你确定你的File?对象不为空,你可以使用空检查操作符!来消除这个错误。

Image.file(_image!),

相关问题