代码应该在容器中显示一张图片,但它没有显示任何内容!而不是文本.我使用了Image.asser(),它可以在容器中显示图像,但当我使用SelectedImage并使用来自图库或相机的Future函数在其中存储图片时,该函数不起作用,也不会在容器中显示任何图片 当我直接提供SelectedImage child: SelectedImage时,我收到以下错误 The argument type 'File?' can't be assigned to the parameter type 'Widget?'

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:project1_test/theme/Textstyle.dart';
import 'package:project1_test/theme/colors.dart';

class ImageSelectionfunction extends StatefulWidget {
  const ImageSelectionfunction({super.key});

  @override
  State<ImageSelectionfunction> createState() => _ImageSelectionfunctionState();
}

class _ImageSelectionfunctionState extends State<ImageSelectionfunction> {
  @override
  Widget build(BuildContext context) {
    File? SelectedImage;
    Future<void> pickImage() async {
      final picker = ImagePicker();
      final pickedImage = await picker.pickImage(source: ImageSource.gallery);
      if (pickedImage != null) {
        setState(() {
          SelectedImage = File(pickedImage.path);
        });
      }
    }

    return Column(
      children: <Widget>[
        Center(
         //The below container is suppose to display the picture
          child: Container( 
            width: 220,
            height: 190,
            decoration: const BoxDecoration(color: Colors.white, boxShadow: [
              BoxShadow(
                color: Colors.grey,
                offset: Offset(0, 4),
                blurRadius: 5,
                spreadRadius: 5,
              )
            ]),
            child: SelectedImage != null
                ? Image.file(File(SelectedImage!.path))
                : const Text('Your Image here'),
          ),
        ),
        SizedBox(
          height: 35,
        ),

        ////////////////////////////
        //Image Selection Buttons
        //////////////////////////
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Container(
              width: 180,
              decoration: BoxDecoration(
                color: purpleColor,
              ),
              child: Row(
                children: [
                  const Padding(
                    padding: EdgeInsets.only(left: 8),
                    child: Icon(
                      FontAwesomeIcons.image,
                      color: Colors.white,
                      size: 17,
                    ),
                  ),
                  TextButton(
                    child: const Text(
                      'Pick From Gallery',
                      style: txtStyle,
                    ),
                    onPressed: () {
                      pickImage();
                    },
                  ),
                ],
              ),
            ),
            Container(
              width: 160,
              decoration: BoxDecoration(
                color: purpleColor,
              ),
              child: Row(
                children: [
                  const Padding(
                    padding: EdgeInsets.only(left: 8),
                    child: Icon(
                      FontAwesomeIcons.camera,
                      color: Colors.white,
                      size: 17,
                    ),
                  ),
                  TextButton(
                    child: const Text(
                      'Take a Picture',
                      style: txtStyle,
                    ),
                    onPressed: () {},
                  ),
                ],
              ),
            ),
          ],
        ),
        ////////////////////////
        //Buttons Area Ends Here
        ////////////////////////

        ///////////////////////
        //Prediction Button
        ///////////////////////
        SizedBox(
          height: 12,
        ),
        Container(
          width: 180,
          height: 60,
          color: purpleColor,
          child: Row(children: [
            Padding(
              padding: const EdgeInsets.only(left: 10),
              child: Icon(
                FontAwesomeIcons.magnifyingGlass,
                color: Colors.white,
                size: 18,
              ),
            ),
            SizedBox(
              width: 12,
            ),
            Text(
              'Start Prediction',
              style: txtStylemain,
            ),
          ]),
        )
        //////////////////////////////
        //Prediction Button Ends Here
        ////////////////////////////
      ],
    );
  }
}

推荐答案

在您的代码中,您在Build方法中放置了SelectedImage个声明.使用setState时,将重新调用Build方法,并使用初始值null重新声明SelectedImage.

要修复它,请将SelectedImage变量移到Build方法之外(将其置于小部件状态).

class _ImageSelectionfunctionState extends State<ImageSelectionfunction> {
  File? SelectedImage;
  
  @override
  Widget build(BuildContext context) {
    // ...
  }
}

Flutter相关问答推荐

Flutter:sqflite DB

如何使用Firebase Firestore来获取外部集合中的文档子集合中的文档

如何在不使用NULL-check(!)的情况下缩小`FutureBuilder.Builder()`内部的`Snaphot`范围

SingleChildScrollView正在将Container的高度限制为子对象,尽管使用了Expanded Inside Row()

错误:找不到字体功能类型.Ffltter Google_Fonts包错误

Dexing时出错.将Flutter 升级到3.19.0后

我如何才能动态地将小部件排成一排进行排列呢?

Android Studio中的Ffltter&;Couchbase:进程C:/Program Files/Git/bin/bash以非零退出值35结束

从超级应用程序启动时,Flutter 小应用程序中未加载资源

FOR回路中的Flutter 模型

参数类型';List<;Dynamic&>不能分配给参数类型';List<;SingleChildWidget&>';

Flutter 需要 Xcode 14 或更高版本

Flutter Dismissible 不会在 startToEnd 滑动时被关闭

Flutter / 为什么当我点击我的图片时我的模型没有更新?

使用 Play Integrity API 时,Firebase 电话身份验证问题缺少客户端标识符错误

Flutter - 异步文件读取异常处理

如何在 flutter 中更改日期 Select 器 colored颜色 ?

Flutter Firestore 数据库排序最近 24 小时内最喜欢的文档

我收到这个错误我无法解决

如何从 Firebase Firestore 中获取具有特定字段值的文档?