[英]Flutter Image Picker doesn't pick image from Album/Gallery
I'm trying to implement imagepicker library in my app.我正在尝试在我的应用程序中实现 imagepicker 库。 My app runs without any errors but I can't pick/select images from the album/gallery.我的应用程序运行没有任何错误,但我无法从相册/图库中挑选/选择图像。 On selecting the image the app should display the image in the body but nothing happens.在选择图像时,应用程序应在正文中显示图像,但没有任何反应。 I have to tap on Cancel to go back to the main screen.我必须点击取消 go 返回主屏幕。 I should also mention that the console returns null
for the print statement print('image: $image');
我还应该提到,控制台返回null
用于打印语句print('image: $image');
Kindly help.请帮忙。
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
brightness: Brightness.dark,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File image;
final picker = ImagePicker();
Future getImage() async {
final pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
if (pickedFile != null) {
image = File(pickedFile.path);
} else {
print('image: $image');
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Picker'),
),
body: Center(
child: image == null
? Text('No image selected.')
: Image.file(image),
),
floatingActionButton: FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: Icon(Icons.add_a_photo),
),
);
}
}
Try with the below code试试下面的代码
Future getImage() async {
File pickedFile = await picker.getImage(source: ImageSource.gallery);
if (pickedFile != null) {
setState(() {
image = File(pickedFile.path);
});
} else {
print('image: $image');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.