微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将从 filepicker_windows 中选择的文件保存在磁盘中Flutter Desktop

如何解决如何将从 filepicker_windows 中选择的文件保存在磁盘中Flutter Desktop

请我需要帮助。 我写了下面的代码来保存从 filepicker_windows 选择的文件到 C: 驱动器。

filepicker_windows 运行良好,它成功获取了我的 C: 驱动器,但没有保存文件。 它给了我错误“发生异常。 NoSuchMethodError (NoSuchMethodError: Class 'String' 没有实例 getter 'bodyBytes'。 接收器:“C:\Users\norak\OneDrive\Pictures\afdb\Capture.PNG” 尝试调用:bodyBytes)"

以下是我的代码,请帮忙改正。

pubspec.yaml 文件

name: file_manager
description: A new Flutter project.

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  Flutter:
    sdk: Flutter

  cupertino_icons: ^1.0.2
  filepicker_windows: ^2.0.0
  path_provider: ^2.0.1
  provider: ^5.0.0

dev_dependencies:
  Flutter_test:
    sdk: Flutter

Flutter:
  uses-material-design: true

主页

import 'dart:io';

import 'package:Flutter/material.dart';
import 'package:filepicker_windows/filepicker_windows.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),);
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<String> getPicturesPath() async {
    Directory docDir = await getApplicationDocumentsDirectory();
    var pathList = docDir.path.split('\\');
    pathList[pathList.length - 1] = 'Pictures';
    var picturePath = pathList.join('\\');
    print(picturePath);
    return picturePath;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("File Management"),),body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
            Text(
              'You have pushed the button this many times:',Text(
              'Testing',style: Theme.of(context).textTheme.headline4,],floatingActionButton: FloatingActionButton(
        onpressed: () async {
          final file = OpenFilePicker()
            ..filterSpecification = {'All Files': '*.*'}
            ..defaultFilterIndex = 0
            ..defaultExtension = 'doc'
            ..title = 'Select a document';

          final result = file.getFile();
          if (result != null) {
            print(result.path);
            saveImage(result.path,'ik.jpg');
            print("Saved");
          }
        },tooltip: 'Increment',child: Icon(Icons.add),// This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void saveImage(imageData,String imageName) async {
    var picturesPath = await getPicturesPath();
    var thetaimage = await File(join(picturesPath,'theta_images',imageName))
        .create(recursive: true);
    await thetaimage.writeAsBytes(imageData.bodyBytes); 
  }
}

await thetaimage.writeAsBytes(imageData.bodyBytes); //这是给出错误的行

请指教

解决方法

你为什么不调试你的代码?这是一个字符串。这就是您收到错误的原因。

enter image description here

将代码更改为:

await thetaImage.writeAsBytes(File(imageData).readAsBytesSync());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。