Reputation: 120529
I have the in-memory bytes of a "blob", but the API that I want to process this "blob" with only accepts dart:io File objects.
Is there a way to create a "fake" dart:io File , simply wrapping my in-memory bytes, so that I can pass this "fake" File to my API?
Assume that a file system doesn't exist, and assume that I can't write the in-memory bytes to a "real" file.
Thanks!
Upvotes: 28
Views: 11166
Reputation: 558
I always use cross_file when designing APIs that work with files.
In addition to working on multiple platforms, it also has an XFile.fromData constructor that allows you to create a file in memory.
Upvotes: 0
Reputation: 366
You can override the io using the IOOverides
class. See the below example:
await IOOverrides.runZoned(() async {
// Write your code here.
},
createDirectory: (p0) {
return MockDirectory()
},
createFile: (p0) {
return MockeFile();
},
);
You can check the doc here for more info.
Upvotes: 0
Reputation: 8427
You can create a memory-file using MemoryFileSystem
from the package file:
Example:
File file = MemoryFileSystem().file('test.dart')
..writeAsBytesSync(blobBytes);
Upvotes: 26
Reputation: 5863
Add path provider dependency on pubspec.yaml
dependencies:
path_provider: 0.2.2
Write byte data to file, use it, then delete it.
import 'dart:io';
import 'package:path_provider/path_provider.dart';
main() async {
String dir = (await getTemporaryDirectory()).path;
File temp = new File('$dir/temp.file');
var bytes = [0, 1, 2, 3, 4, 5];
await temp.writeAsBytes(bytes);
/*do something with temp file*/
temp.delete();
}
Upvotes: 12