問題描述
無法在 iOS 模擬器上使用顫振圖像選擇器選擇圖像 (Unable to select image using flutter image picker on iOS simulator)
我正在學習如何從圖庫中選擇圖像。我已經實現了同樣的顫振圖像選擇器。但是當我嘗試在模擬器中選擇圖像時,我無法這樣做。
請幫助
Flutter Doctor
Doctor summary (to see all details, run flutter doctor ‑v):
[✓] Flutter (Channel beta, 1.26.0‑17.3.pre, on macOS 11.1 20C69 darwin‑arm, locale en‑IN)
[✓] Android toolchain ‑ develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode ‑ develop for iOS and macOS
[✓] Android Studio (version 4.1)
[✓] Connected device (1 available)
• No issues found!
我的代碼
import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(MaterialApp(
home: Play(),
// theme: ThemeData.dark(),
));
}
class Play extends StatefulWidget {
@override
_PlayState createState() => _PlayState();
}
class _PlayState extends State<Play> {
File _file;
ImagePicker _picker = ImagePicker();
PickedFile _pickedFile;
Future _getImageFromGallery() async {
print("Getting Image from Gallery.");
_pickedFile = await _picker.getImage(source: ImageSource.gallery);
print(_pickedFile.path);
setState(() {
_file = File(_pickedFile.path);
});
}
Future _getImageFromCamera() async {
print("Getting Image from Camera.");
_pickedFile = await _picker.getImage(source: ImageSource.camera);
setState(() {
_file = File(_pickedFile.path);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Play"),
),
body: Column(
children: [
Center(
child: ElevatedButton(
onLongPress: _getImageFromCamera,
onPressed: _getImageFromGallery,
child: Text("Get Image")),
),
_file != null
? Image.file(
_file,
height: 200,
)
: Icon(
Icons.image,
size: 100,
),
],
),
);
}
}
我正在使用 M1 Mac air。我已切換到 beta 頻道以擺脫一些冗長的錯誤。一切正常。只是,當我單擊圖像時,什麼也沒有發生。它沒有被選中。
參考解法
方法 1:
I've tried your code and it works on my simulator:
There are discussions online including this GitHub post, that this could be an Apple bug and no known workaround.
In fact, there are people in Apple Developer Forums reports this kind of issue.
(by OM KALE、MαπμQμαπkγVπ.0)