问题描述
请我需要帮助。 我写了下面的代码来保存从 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); //这是给出错误的行
请指教