Converting file path to file in Flutter - flutter

I am getting the path of an image from the device storage, like this:
path = res[0]?.path;
print("path:"+path);
The output of that print is:
/data/user/0/qplan/cache/image_picker_b432f88b-3146-4a99-9e8b-acbefd066e3a2471538209034937554.jpeg
I need to convert it to File in order to upload the image to Firestore Storage.

You can get file inserting your path to File object as below;
File fileToUpload = new File(path);

You can use this.
import 'dart:async';
import 'dart:io';
path = res[0]?.path;
File(path).readAsString().then((String contents) {
print(contents);
});

Related

Flutter how to remove file extension when i need to put file name into variable

I use p.basename from flutter path library to get file name, but i got it with extension, how to remove extension? also to get path I use getApplicationDocumentsDerectory from path_provider library
File newFile = File(filepath);
String name = p.basename(newFile.path);
this is code I use to get file name
You just use 'basenameWithoutExtension' method like basename.
File newFile = File(filepath);
String name = p.basename(newFile.path);
String nameWithoutExtension = p.basenameWithoutExtension(newFile.path);

How to get base64 of a file in Flutter

I have an file path string like
path = /data/user/0/com.digitalpathshalabd.school/cache/Shaiful_Islam.docx
now I want to convert the file into base64
How could I achieve this ?
Finally I come up with a solution.
The thing is we have to get the actual file from path before converting it.
get the actual file
convert the file into byte array
finaly convert the byte array to base64
import 'dart:convert';
import 'dart:io';
class FileConverter {
static String getBase64FormateFile(String path) {
File file = File(path);
print('File is = ' + file.toString());
List<int> fileInByte = file.readAsBytesSync();
String fileInBase64 = base64Encode(fileInByte);
return fileInBase64;
}
}

Convert base64 to image and save it in temp folder flutter

I have base64 string of image like /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikxMC4pLSwzOko+MzZGNywtQFdBRkxOUlNSMj5aYVpQYEpRUk//....
What I want to do is to save this image in temp folder and use that file address for showing image in my app.
How can I do that?
import 'package:path_provider/path_provider.dart' as syspaths;
Decode your base64 string to bytes in memory.
Uint8List bytes = base64.decode(base64String);
Make a temporary directory and file on that directory
final appDir = await syspaths.getTemporaryDirectory();
File file = File('${appDir.path}/sth.jpg');
Write converted bytes on a file
await file.writeAsBytes(bytes)
then we can
Image.file(file);
OR ALTERNATIVELY
Decode your base64 string to bytes in memory.
Uint8List bytes = base64.decode(base64String);
then we can
Image.memory(bytes)

File cant be assigned with ImagePicker Flutter

i am learning imagepicker from https://pub.dev/packages/image_picker ,
but i don't why i got an error when i have use the way step by step.
this is the problem :
first, i declare a File variable
File _imageFile ;
then i use it in a method,
_getimg() async{
var _img = await ImagePicker(source: ImageSource.gallery);
setState(() {
_imageFile = _img ;
});
}
and then i got this error :
A value of type 'File (where File is defined in
D:\Flutter\flutter\bin\cache\pkg\sky_engine\lib\io\file.dart)' can't
be assigned to a variable of type 'File (where File is defined in
D:\Flutter\flutter\bin\cache\pkg\sky_engine\lib\html\html_dart2js.dart)'.
There is a conflict between Files declaration. The html package has one declaration of a class File and the io package has another declaration (same name, different origin).
In fact, using html is for web and io is used for console, server or mobile apps, so check your imports and delete io or html depending in the type of project you are working on.
Another solution is to define your imports like this:
import 'package:html/html.dart' as h; //"h" can change, is just an example
import 'dart:io' as i; //"i" also can be another char or word, is just an example
//And when you need to create a File,
//you can decide if you want to create
//an io File or an html File
main(){
i.File f1 = ...; //The io File, starting with "i."
h.File f2 = ...; //The html File, starting with "h."
}

Get name of test-file inside of flutter test

Assume I have a flutter test file foo_test.dart with:
void main() {
final file = ...how to get the path of this file... 'foo_test.dart';
...
}
How can I achieve that?
The following snippet allows to retrieve the file containing main():
final mainFile = Uri.parse(RegExp(r'#.*main \((.*):.*:.*\)')
.firstMatch(StackTrace.current.toString())
.group(1))
.toFilePath();