How to convert type String to type File in Flutter - flutter

I am running a project with flutter-web
This time is first time doing project with flutter-web. and now i just knew that flutter web doesn't supports Dart:io
So i am trying to use dart.html and... it is quite different compare to dart:io
uploadImage(ReservationPvd reservationPvd) async {
final XFile? photo = await ImagePicker().pickImage(source: ImageSource.gallery);
File image = File(photo!.path);
MultipartFile imageFile = await MultipartFile.fromFile(photo.path);
reservationPvd.updateInfo('image', image);
reservationPvd.updateInfo('imageFile', imageFile);
}
this following code is the code that i've used in my other project with flutter.
and I need to save this File to Type File and MultipartFile
File image = File(photo!.path);
and this following code gives an error message, and it says
The argument type 'String' can't be assigned to the parameter type 'List<Object>'
i have googled for a 3 days but i really can't solve this problem...
does anyone knows how to upload fine ways to upload in flutter-web?

I have flutter-web project and it use MultipartFile.fromBytes not MultipartFile.fromFile. Because in flutter-web you can't get File from path.
Try this:
uploadImage(ReservationPvd reservationPvd) async {
final XFile? photo = await ImagePicker().pickImage(source: ImageSource.gallery);
MultipartFile imageFile = MultipartFile.fromBytes(await photo!.readAsBytes());
reservationPvd.updateInfo('image', image);
reservationPvd.updateInfo('imageFile', imageFile);
}

I suspect your File class may be imported from the wrong dependency. It should use File from dart:io like so:
import 'dart:io';
And it's likely you're currently using dart:html:
import 'dart:html';
Auto import suggests dart:html over dart:io for me as well, so this is the most likely cause I think.

to get around dart io not supported in web
after you pick the image instead of File(file.path) you say file.readAsBytes(); the you store the output of file.readAsBytes() in a Uint8List variable. when you want to display the images you use MemoryImage() provided in flutter

Related

How to convert a pdf file (composed of different images) to list of images in flutter?

I am developeing a PDF converter flutter app it converts images into pdf file. I want to expand it so that it should take pdf file and convert it into list of images so that i can rearrange order of images or remove a specific image form pdf. How can i solve this issue.I tried multiple packages but didn't find a solution.
Anyone can help me?
Thanks!
I am developeing a PDF converter flutter app it converts images into pdf file. I want to expand it so that it should take pdf file and convert it into list of images so that i can rearrange order of images or remove a specific image form pdf. How can i solve this issue.I tried multiple packages but didn't find a solution. Anyone can help me? Thanks!
Try to use PDFTron SDK package.
pdftron_flutter: ^0.5.0
or import this:
import 'package:pdftron_flutter/pdftron_flutter.dart';
import 'package:pdftron/pdftron.dart';
Initialize first:
await PDFNet.initialize();
methhods:
var pdfDoc = await PDFDoc.fromFilePath(filePath);
var page = await pdfDoc.getPage(pageNumber);
var resources = await page.getResources();
var image = await resources.getImage(imageName);
var imageData = await image.getImageData();
convert the image data to a flutter using Image.memory
var imageWidget = Image.memory(imageData);

I want to upload excel file on cloud firestore using flutter

I want to upload excel file in flutter and on button click data in file should be store in cloud firestore. Kindly provide me code as I have no idea in this field.
Consider using Firebase Storage instead of Firestore for saving files. You can then store the download link in Firestore.
For upload you can check:
Note: You must first get the absolute path to its on-device location.
Directory appDocDir = await getApplicationDocumentsDirectory();
String filePath = '${appDocDir.absolute}/file-to-upload.png';
File file = File(filePath);
try {
await mountainsRef.putFile(file);
} on firebase_core.FirebaseException catch (e) {
// ...
}
for further reference [Here is the documentation][1]
[1]: https://firebase.google.com/docs/storage/flutter/upload-files

Load docx file from assets in Flutter

I have a terms and conditions .docx document, If you have a crypto.com account you see in the terms and conditions they essentially just load a docx document out of somewhere. I'd like to load mine from my assets using file_reader but it takes the full path of the file, not the relative assets file.
I guess the real question is where can I store this file, so when I compile it, I can then somehow figure out the path of the file, and then feed it to the FileReaderView.
Try to use: open_file. It may solve your problem, they use a relative path.
Future<File> openFileFromAsset() async {
Directory tempDir = await getTemporaryDirectory();
File tempFile = File('${tempDir.path}/set_any_name.docx');
ByteData bd = await rootBundle.load('assets/en/legal_notes.docx');
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}
openFileFromAsset().then((file){OpenFile.open(file.path);});
Also update your pubspec.yaml file:
flutter:
assets:
- assets/files/samplefile.docx

Flutter: Error: MissingPluginException(No implementation found for method getTemporaryDirectory on channel plugins.flutter.io/path_provider)

I'm using Flutter-web and I want to export a pdf. I'm using the pdf package and i'm trying to implement a simple example from their documentation. To be more specific, I have a file called export_pdf.dart and the code inside it is the following.
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
import 'package:universal_io/io.dart';
import 'package:path_provider/path_provider.dart';
exportPdf() async {
final pdf = Document();
pdf.addPage(Page(
pageFormat: PdfPageFormat.a4,
build: (Context context) {
return Center(
child: Text("Hello World"),
); // Center
})); // Page
final output = await getTemporaryDirectory();
final file = File("${output.path}/example.pdf");
await file.writeAsBytes(await pdf.save());
}
When i'm calling the exportPdf() function by clicking a button, i'm getting the following error.
Uncaught (in promise) Error: MissingPluginException(No implementation
found for method getTemporaryDirectory on channel
plugins.flutter.io/path_provider)
I've been searching for this issue for a long time, but no solution has fixed this one.
Even though the path_provider package is imported, the getTemporaryDirectory() is never called, like it doesn't exist.
I need also to mention that i'm using universal_io, instead of dart:io, because i'm using flutter_web.
This error shows up for every function i'm calling and exists inside the path_provider/path_provider.dart file. I've also added inside path_provider/path_provider.dart a simple print function and i'm getting an error that the method is not found.
Thank you for your time.
Run these commands
flutter clean
flutter pub get
flutter run
Make sure after the first command, the build folder is removed.
I found out that this current package does not support file saving in web and it is suggested to use the printing plugin to print or share the file.

How can I read and Write Files in Flutter Web?

I am working on flutter-web, I would like to do file operations(read and write) in flutter-web.
For Android and IOS, I was using path_provider dependency. But in Flutter-web, it is not supported.
Can anyone help me with this?
The accepted answer is not completely right. Yes, dart:io is not available on the web, but it is still possible to read files. You can select a file through the system's file picker and read it afterward.
An easy option to "write" a file is to send the user an auto-download.
Read:
Use this library to select a file: pub.dev/packages/file_picker (Web migration guide)
import 'dart:html' as webFile;
import 'package:file_picker_web/file_picker_web.dart' as webPicker;
if (kIsWeb) {
final webFile.File file = await webPicker.FilePicker.getFile(
allowedExtensions: ['pd'],
type: FileType.custom,
);
final reader = webFile.FileReader();
reader.readAsText(file);
await reader.onLoad.first;
String data = reader.result;
}
Write (a.k.a download):
import 'dart:html' as webFile;
if (kIsWeb) {
var blob = webFile.Blob(["data"], 'text/plain', 'native');
var anchorElement = webFile.AnchorElement(
href: webFile.Url.createObjectUrlFromBlob(blob).toString(),
)..setAttribute("download", "data.txt")..click();
}
in the dart docs it has been explained ... dart:io is not available to web build ... it means that you can not directly access to system files in flutter web . the build will fail .. you need to remove the code in order to run it
this is a security thing .. js doesn't allow it too .. imagine a website that can read all your system files if you visit it .. that's why things like path reading or file reading is not allowed .. it goes same for flutter
hope you have your answer
I created a package specifically for this before Flutter web was a thing. https://pub.dev/packages/async_resource
It looks at things as either a network resource (usually over HTTP), or a local resource such as a file, local storage, service worker cache, or shared preferences.
It isn't the easiest thing in the world but it works.
What about "dart:indexed_db library", You can store structured data, such as images, arrays, and maps using IndexedDB. "Window.localStorage" can only store strings.
dart:indexed_db
Window.localStorage
When it comes specifically to reading a file from disk on web, the easiest way is to use a PickedFile like so:
PickedFile localFile = PickedFile(filePath);
Uint8List bytes = await localFile.readAsBytes();