【问题标题】:flutter image picker not working and crashing app without error on debug颤动的图像选择器不工作并且在调试时崩溃的应用程序没有错误
【发布时间】:2022-07-05 04:56:26
【问题描述】:

我目前正在使用这些版本: 颤振:2.16.0 image_picker : ^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'))
                    
                  ],
                )),
              ),
            ),
          ),
        ),
      ),
    );
    }
    }

【问题讨论】:

  • 您是否在 manifest.xml 中添加了“android:requestLegacyExternalStorage="true"”??

标签: flutter imagepicker


【解决方案1】:

将此添加到 iOS>runner>Info.plist

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

【讨论】:

    【解决方案2】:

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

    如果你已经正确注册了 ImagePicker,然后在 pick Image Function 中尝试并捕获博客以获取更多调试信息:

    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");
      }
    
    } 
    

    【讨论】:

      【解决方案3】:

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

      NSPhotoLibraryUsageDescription NSCameraUsageDescription

      还可以尝试在物理设备中测试您的应用。如果您使用 Android 版本,则无需更改配置,您可以参考此视频:https://youtu.be/s0YqbEJcRtE

      【讨论】:

      • 您可以使用相机:^0.9.5 是更好的选择
      【解决方案4】:

      为了在苹果芯片 (M1) 上进行开发,我需要删除 google_maps 插件。此插件将模拟器移至 Rosetta,这在模拟器中的图像处理方面存在一些问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-15
        • 2020-11-15
        • 1970-01-01
        相关资源
        最近更新 更多