flutter图像选取器不工作,并在调试时崩溃应用程序而没有错误



我目前正在使用以下版本:颤振:2.16.0图像拾取:^0.8.4+7

图像选取器不工作。运行应用程序后,当我点击按钮激活pickImage功能时,运行突然停止,应用程序崩溃并停止。在调试时,我得到的唯一消息是:

与设备的连接丢失。

这是代码:

import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hipocampus_editors/widgets/textformfield_widget.dart';
import 'package:image_picker/image_picker.dart';
class AddSystemPage extends StatefulWidget {
const AddSystemPage({Key? key}) : super(key: key);
@override
_AddSystemPageState createState() => _AddSystemPageState();
}
class _AddSystemPageState extends State<AddSystemPage> {
final _formKey = GlobalKey<FormState>();
File? image1;
Future pickImage() async{

final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null)  return;
final imageTemporary = File(image.path);
setState(() {
image1 = imageTemporary;
});
} 
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
child: Scaffold(
appBar: AppBar(title: const Text('System',),),

body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 5),
child: Form(
key: _formKey,
child: Container(
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.symmetric(horizontal: 10),
child: SingleChildScrollView(
child: Column(
children: [

ElevatedButton(onPressed: (){
pickImage();
}, child: Text('Select image'))

],
)),
),
),
),
),
),
);
}
}

将其添加到iOS>转轮>信息列表

<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>

请检查您是否正确注册了ImagePicker。

如果你已经正确注册了ImagePicker,那么在pick Image Function中放一个try-and-catch博客,以获得更多调试信息:

Future pickImage() async{

try{
final image = await ImagePicker().pickImage(source: ImageSource.gallery);
if (image == null)  return;
final imageTemporary = File(image.path);
setState(() {
image1 = imageTemporary;
});
} catch(error) {
print("error: $error");
}
} 

这发生在我尝试使用iOS模拟器时。您需要在/ios/Runner/Info.plist:中添加这些密钥

NSPhotoLibrary使用说明NSCamera使用说明

同时尝试在物理设备中测试您的应用程序。如果你为Android版本工作,那么不需要更改配置,你可以参考此视频:https://youtu.be/s0YqbEJcRtE

为了在苹果硅(M1(上进行开发,我需要删除google_maps插件。这个插件将模拟器移动到罗塞塔,这在模拟器中的图像处理方面存在一些问题。

最新更新