0

Please I need assistance. I have written below code to saved file selected from filepicker_windows to C: drive.

filepicker_windows worked well and it get my C: drive successfully But not saving the file. It gives me ERROR "Exception has occurred. NoSuchMethodError (NoSuchMethodError: Class 'String' has no instance getter 'bodyBytes'. Receiver: "C:\Users\norak\OneDrive\Pictures\afdb\Capture.PNG" Tried calling: bodyBytes)"

Below is my code, please help correct it.

pubspec.yaml File

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

MAIN PAGE

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); //This is the line that is given error

Please advice

noble
  • 11
  • 1

2 Answers2

0

Why don't you debug your code? It's a string. That's why you got the error.

enter image description here

Change the code to:

await thetaImage.writeAsBytes(File(imageData).readAsBytesSync());
yushulx
  • 11,695
  • 8
  • 37
  • 64
0
>     final directory = await getApplicationDocumentsDirectory();
        final path = directory.path;
        final file = File(path);
fkode
  • 11
  • 3