flutter 在iOS和Android上拾取和预览图像时出现问题

xxhby3vn  于 7个月前  发布在  Flutter
关注(0)|答案(1)|浏览(97)

我正在使用Flutter image_picker库来实现一个功能,用户可以选择一个图像并将其上传到后端服务器。一旦他们点击选择一个图像,一个对话框应该询问他们是否要从图库中加载图像或拍摄一个。
然而,根据我的实现,我收到了一个奇怪的异常,
第一个月
我已经检查了我的功能,但我不明白问题在哪里。你能帮助指导吗?下面是代码。
非常感谢您!

List<File?> images = List.generate(3, (index) => null);

  Future<void> _getImage(int index) async {
    XFile? image = await showDialog<XFile?>(
      context: context,
      builder: (context) => const ImageSourceDialog(),
    );

    if (image != null) {
      setState(() {
        images[index] = File(image.path);
      });
    }
  }

class ImageSourceDialog extends StatelessWidget {
  const ImageSourceDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text("Choissisez une source"),
      content: Row(
        children: [
          Expanded(
            child: ListTile(
              dense: true,
              leading: const Icon(Icons.camera),
              title: const Text("Prendre une photo"),
              onTap: () {
                Navigator.of(context).pop(
                  ImagePicker().pickImage(source: ImageSource.camera),
                );
              },
            ),
          ),
          Expanded(
            child: ListTile(
              dense: true,
              leading: const Icon(Icons.photo),
              title: const Text("Choisir une photo"),
              onTap: () {
                Navigator.of(context).pop(
                  ImagePicker().pickImage(source: ImageSource.gallery),
                );
              },
            ),
          )
        ],
      ),
    );
  }
}

// this is the widget in the stateful class that triggers the upload function

iconButton(onPress: () {_getImage(index)}, ...);

字符串

pvcm50d1

pvcm50d11#

`import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class MyImagePick extends StatefulWidget {
const MyImagePick({super.key});
static   List<File?> images = List.generate(3, (index) => 
null);
@override
State<MyImagePick> createState() => _MyImagePickState();
}
class _MyImagePickState extends State<MyImagePick> {
@override
Widget build(BuildContext context) {
return Scaffold(appBar: AppBar(),
body: Column(children: [ElevatedButton(onPressed: (){
_getImage(0,context);
}, child: Text("pick image"))],),
);
}
}

Future<void> _getImage(int index,context) async {
XFile? image = await showDialog<XFile?>(
context: context,
builder: (context) => const ImageSourceDialog(),
);

if (image != null) {

MyImagePick.images[index] =await File(image.path);

}
 }

 class ImageSourceDialog extends StatelessWidget {
 const ImageSourceDialog({super.key});

 @override
 Widget build(BuildContext context) {
 return AlertDialog(
  title: const Text("Choissisez une source"),
  content: Row(
    children: [
      Expanded(
        child: ListTile(
          dense: true,
          leading: const Icon(Icons.camera),
          title: const Text("Prendre une photo"),
          onTap: () {
            Navigator.of(context).pop(
              ImagePicker().pickImage(source: ImageSource.camera),
            );
          },
        ),
      ),
      Expanded(
        child: ListTile(
          dense: true,
          leading: const Icon(Icons.photo),
          title: const Text("Choisir une photo"),
          onTap: () {
            Navigator.of(context).pop(
              ImagePicker().pickImage(source: ImageSource.gallery),
            );
          },
        ),
      )
    ],
  ),
);
}
 }`

字符串
在await文件之前使用await(image.path)

相关问题