未定义的名称' flutterabsolutepath'

发布于 2025-02-05 11:53:13 字数 286 浏览 3 评论 0原文

我正在开发一个扑通移动应用程序,需要通过API上传多个图像。现在,我完成了从画廊挑选图像的部分。下一步是上传图像。为此,我需要图像路径。我知道可以通过使用以下代码行来获得图像路径,但它一直说未定义的名称'FlutterAbSolutePath'如何解决此问题的原因是什么?我正在使用多图像挑战器包。

final filePath =
          await FlutterAbsolutePath.getAbsolutePath(image.identifier);

I am developing a flutter mobile application where I need to upload multiple images through an API. Now I completed the part where pick images from the gallery. Next step is to upload the images. To do that I need the image path. I know that image path can be get by using the below code line but it keep saying that Undefined name 'FlutterAbsolutePath' what is the reason how to fix this issue. I am using multi-image-picker package.

final filePath =
          await FlutterAbsolutePath.getAbsolutePath(image.identifier);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-02-12 11:53:13

如果您可以为我们提供更多信息甚至代码示例,那真的很好。如果您正在使用此 package ,我强烈建议您将其更改为不再不受欢迎的一个(看看这个: image_picker ;您也可以一次选择多个图像) 。

在提供更多信息之前,您可以查看以下代码,该代码使用 image_picker
遵循文档的安装步骤后,您可以使用下一个方法获得图像:

Future<void> getImages() async {
  ImagePicker imagePicker = ImagePicker();

  List<XFile>? images = await imagePicker.pickMultiImage();

  if ((images ?? []).isNotEmpty) {
    //get the path of the first image from the select images
    String imagePath = images?.first.path ?? '';

    //get the content of the first image from the select images as bytes
    Uint8List? imageAsBytes = await images?.first.readAsBytes();

    //get the content of the first image from the select images as a String based on a given encoding
    String? imageAsString = await images?.first.readAsString();
  }
}

我希望我对您有所帮助。愉快的编码! :)

后来的编辑:

这是一个非常基本的幻影应用程序,它可以使用多件式挑选图像创建一个临时文件,我们知道 path 并显示临时文件为了证明正确显示临时文件。

对您来说,最有趣的是getfile()方法,它照顾创建带有摘要图像的1:1的临时文件。

为了创建临时文件,我使用了: path_provider 也请查看以下内容:读写文件

用于许可处理: cermission_handler

我仍然强烈建议您使用其他软件包进行图像选择,因为该摘要已停止:)。

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

Future<File?> getFile() async {
  List<Asset> images = await MultiImagePicker.pickImages(maxImages: 100);

  if (images.isNotEmpty) {
    ByteData bytesData = await images.first.getByteData();
    List<int> bytes = bytesData.buffer
        .asUint8List(bytesData.offsetInBytes, bytesData.lengthInBytes)
        .cast<int>();

    //get the extension jpeg, png, jpg
    String extension = (images.first.name ?? '').split('.').last;

    //get path to the temporaryDirector
    final Directory directory = await getTemporaryDirectory();

    //get the path to the chosen directory
    String directoryPath = directory.path;

    //create a temporary file to the specified path
    //you can use the path to this file for the API
    File file = File('$directoryPath/myImage.$extension');

    //write the bytes to the image
    file.writeAsBytesSync(bytes, mode: FileMode.write);

    //just for path: file.path -> will return a String with the path
    return file;
  }
  return null;
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  File? file;
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.amber,
      child: Column(
        children: [
          const SizedBox(
            height: 50,
          ),
          Center(
            child: TextButton(
              child: const Text('Pick an image'),
              onPressed: () async {
                await Permission.accessMediaLocation.request();
                file = await getFile();

                setState(() {});
              },
            ),
          ),
          const SizedBox(
            height: 50,
          ),
          file != null
              ? Image.file(
                  file!,
                  height: 200,
                  width: 200,
                )
              : Container(),
        ],
      ),
    );
  }
}

It would be really nice if you could provide us with some more information or even a code sample. In case you are using this package, I would strongly advice you to change it to one that is not discontinued (take a look into this one: image_picker; you can select multiple images at once with this one too).

Until further information is provided you can take a look over the following code that uses the image_picker:
After following the installation steps from the documentation you can get images using the next method:

Future<void> getImages() async {
  ImagePicker imagePicker = ImagePicker();

  List<XFile>? images = await imagePicker.pickMultiImage();

  if ((images ?? []).isNotEmpty) {
    //get the path of the first image from the select images
    String imagePath = images?.first.path ?? '';

    //get the content of the first image from the select images as bytes
    Uint8List? imageAsBytes = await images?.first.readAsBytes();

    //get the content of the first image from the select images as a String based on a given encoding
    String? imageAsString = await images?.first.readAsString();
  }
}

I hope that I helped you a little bit. Happy coding! :)

Later edit:

Here is a very basic Flutter app that picks an image with multi-picker, creates a temp file for which we know the path and displays the temp file in order to show that the temp file was correctly displayed.

For you, what's of most interest is getFile() method which takes care of creating the temp file that is 1:1 with picked image(s).

For creating the temp file I used: path_provider also take a look into the following: Read and write files

For permission handling: permission_handler

I still strongly suggest that you use other package for image picking as this one is discontinued :).

import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

Future<File?> getFile() async {
  List<Asset> images = await MultiImagePicker.pickImages(maxImages: 100);

  if (images.isNotEmpty) {
    ByteData bytesData = await images.first.getByteData();
    List<int> bytes = bytesData.buffer
        .asUint8List(bytesData.offsetInBytes, bytesData.lengthInBytes)
        .cast<int>();

    //get the extension jpeg, png, jpg
    String extension = (images.first.name ?? '').split('.').last;

    //get path to the temporaryDirector
    final Directory directory = await getTemporaryDirectory();

    //get the path to the chosen directory
    String directoryPath = directory.path;

    //create a temporary file to the specified path
    //you can use the path to this file for the API
    File file = File('$directoryPath/myImage.$extension');

    //write the bytes to the image
    file.writeAsBytesSync(bytes, mode: FileMode.write);

    //just for path: file.path -> will return a String with the path
    return file;
  }
  return null;
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  File? file;
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.amber,
      child: Column(
        children: [
          const SizedBox(
            height: 50,
          ),
          Center(
            child: TextButton(
              child: const Text('Pick an image'),
              onPressed: () async {
                await Permission.accessMediaLocation.request();
                file = await getFile();

                setState(() {});
              },
            ),
          ),
          const SizedBox(
            height: 50,
          ),
          file != null
              ? Image.file(
                  file!,
                  height: 200,
                  width: 200,
                )
              : Container(),
        ],
      ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文