【问题标题】:How to save File Selected from filepicker_windows in Disk (Flutter Desktop)如何在磁盘中保存从 filepicker_windows 中选择的文件(Flutter Desktop)
【发布时间】:2021-09-11 20:24:44
【问题描述】:

我需要帮助。 我已将以下代码写入从 filepicker_windows 中选择的保存文件到 C: 驱动器。

filepicker_windows 运行良好,它成功获取了我的 C: 驱动器但没有保存文件。 它给了我错误“发生了异常。 NoSuchMethodError(NoSuchMethodError:类'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); 
  }
}

等待 thetaImage.writeAsBytes(imageData.bodyBytes); //这是给出错误的行

请指教

【问题讨论】:

    标签: flutter dart flutter-desktop


    【解决方案1】:

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

    将代码改为:

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

    【讨论】:

      【解决方案2】:
      >     final directory = await getApplicationDocumentsDirectory();
              final path = directory.path;
              final file = File(path);
      

      【讨论】:

        猜你喜欢
        • 2020-01-04
        • 2011-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多