图片在图像选择器中没有被选中。

huangapple go评论48阅读模式
英文:

Image is not picked in imagepicker

问题

我正在尝试使用图像选择器,从相册中选择一张图片,并在选择后将其更改为给定的圆形头像。

import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:image_picker/image_picker.dart';

class AddDoctor extends StatefulWidget {
  @override
  State<AddDoctor> createState() => AddDoctorState();
}

class AddDoctorState extends State<AddDoctor> {
  File? _image;

  pickImage() async {
    final ImagePicker imagePicker = ImagePicker();

    final galleryFile = await imagePicker.pickImage(source: ImageSource.gallery) as File;

    if (galleryFile == null) {
      return galleryFile.readAsBytes();
    } else {
      print('No Image Selected');
    }

    setState(() {
      _image = galleryFile;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFD9E4EE),
      appBar: AppBar(
        title: const Text('Add Doctor'),
        actions: [
          IconButton(
            icon: const Icon(Icons.menu),
            onPressed: () {},
          ),
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: <Widget>[
          CircleAvatar(
            radius: 40,
            backgroundImage: _image != null ? FileImage(_image!) : null,
          ),
          const SizedBox(height: 10),
          ElevatedButton(
            onPressed: () {
              pickImage();
            },
            child: const Text('Upload Image'),
          ),
          const SizedBox(height: 10),
        ],
      ),
    );
  }
}

但是在调试控制台中显示了问题,如“Image not selected”(这是在开始时设置的 if 条件中),以及类似以下的错误:Unhandled Exception: type 'XFile' is not a subtype of type 'File' in type cast

英文:

I am trying to work with image picker where I will pick an image from gallery and it will change in aa given circle avatar when picked.

import &#39;package:cloud_firestore/cloud_firestore.dart&#39;;
import &#39;package:flutter/material.dart&#39;;
//import &#39;package:firebase_core/firebase_core.dart&#39;;
import &#39;package:firebase_auth/firebase_auth.dart&#39;;
import &#39;package:image_picker/image_picker.dart&#39;;

class AddDoctor extends StatefulWidget {
  @override
  State&lt;AddDoctor&gt; createState() =&gt; AddDoctorState();
}

class AddDoctorState extends State&lt;AddDoctor&gt; {
  File? _image;

  pickImage() async {
    final ImagePicker imagePicker = ImagePicker();

    final galleryFile =
        await imagePicker.pickImage(source: ImageSource.gallery) as File;

     if (galleryFile == null) {
       return galleryFile.readAsBytes();
     } else {
       print(&#39;No Image Selected&#39;);
     }

    setState(() {
      _image = galleryFile;
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFD9E4EE),
      appBar: AppBar(
        title: const Text(&#39;Add Doctor&#39;),
        actions: [
          IconButton(
            icon: const Icon(Icons.menu),
            onPressed: () {},
          ),
        ],
      ),
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: &lt;Widget&gt;[
          CircleAvatar(
            radius: 40,
            backgroundImage: _image != null ? FileImage(_image!) : null,
          ),
          const SizedBox(height: 10),
          ElevatedButton(
            onPressed: () {
              pickImage();
            },
            child: const Text(&#39;Upload Image&#39;),
          ),
          const SizedBox(height: 10),
          
        ],
      ),
    );
  }

}

But in the debug console it's showing problem like Image not selected (which is set in my if condition at the beginning) and something like: Unhandled Exception: type 'XFile' is not a subtype of type 'File' in type cast

答案1

得分: 0

final galleryFile = await imagePicker.pickImage(source: ImageSource.gallery) as File;
这里的 "as File" 类型转换引发了问题,因为选择的图像是 Object 类型的 XFile,而你将其声明为 File 对象,所以会出现类型错误,即类型 'XFile' 不是类型 'File' 的子类型。
你可以进行转换或像这样使用文件路径:

final pickedXFile = await imagePicker.pickImage(source: ImageSource.gallery);

final galleryFile = File(pickedXFile.path);
英文:
final galleryFile =
    await imagePicker.pickImage(source: ImageSource.gallery) as File;

here "As file" type cast is causing the problem because
The image picked is type of Object XFile and you're declaring it a File object so it's giving error type 'XFile' is not a subtype of type 'File' in type cast.
you can convert or use file path like this

final pickedXFile = await imagePicker.pickImage(source: ImageSource.gallery);

final galleryFile = File(pickedXFile.path);

huangapple
  • 本文由 发表于 2023年2月14日 22:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定