英文:
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 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
//import 'package:firebase_core/firebase_core.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),
],
),
);
}
}
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论