Unhandled Exception: type 'List<Set<Future<File>>>' is not a subtype of type 'List<File>' in type cast - flutter

I would compress a list of image file before uploading, but i have some doubts to convert type after using map a list of file, it return type List<Set<Future>>, not type List as I expect. How can I convert/cast to List of File in this situation, the compressImage function worked well with only one file. Thank you so much.
import 'package:image/image.dart' as Im;
import 'package:uuid/uuid.dart';
List<File> compressImageList(List<File> tempfileList) async {
List<Future<File>> compressedFileList =
tempfileList.map((file) => {compressImage(file)}).toList();
return compressedFileList ;
}
Future<File> compressImage(File tempFile) async {
String postId = Uuid().v4();
final tempDir = await getTemporaryDirectory();
final path = tempDir.path;
Im.Image? imageFile = Im.decodeImage(tempFile.readAsBytesSync());
final compressedImageFile = File('$path/img_$postId.jpg')
..writeAsBytesSync(Im.encodeJpg(imageFile!, quality: 85));
tempFile = compressedImageFile;
return tempFile;
}
i edit my function as below and it works , thank you.
Future<List<File>> compressImageList(List<File> tempfileList) async {
List<File> compressedFileList = [];
await Future.wait(tempfileList.map((file) async {
var compressImage2 = await compressImage(file);
compressedFileList.add(compressImage2);
}).toList());
return compressedFileList;
}

Not sure, haven't tested it, but try removing the {} on the 6th line (as shown above). These brackets signify that it is a type set, which is being returned by =>. If using => for a single statement you don't need to use brackets.

Do it like this;
List<File> compressImageList(List<File> tempfileList) {
List<File> compressedFiles = [];
tempfileList.forEach((file) async {
final compressedFile = await compressImage(file);
compressedFiles = [...compressedFiles, compressedFile];
});
return compressedFiles;
}

Related

save map locally and use it elsewhere

I'm converting a map to a string in order to save it to the device memory
_read() async {
try {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/answers.txt');
String text = await file.readAsString();
print(text);
} catch (e) {
print("Couldn't read file");
}
}
_save() async {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/answers.txt');
await file.writeAsString(answers.toString());
print('saved');
}
now I want to use it as a map to access the data on the map. is there a way to do it?
my map looks like this {Everyone should read...: Harry Potter, Two truths and a lie...: something, I can quote every line from...: the alchemist}
What you want is a JSON file.
JSON is a special syntax that can be used to store maps and lists in a file.
There is a catch though: You may only store maps and lists of primitive values like string, int or bool, a custom class, for example, cannot be stored in a JSON file. You would have to convert it into a map first.
In order to turn a JSON string into a map, you can use the jsonDecode function. Similarly, the jsonEncode function will return a string from a map.
Here goes the code:
Future<Map<String, dynamic>> _read() async {
final file = File(filePath);
final jsonStr = await file.readAsString()
return jsonDecode(jsonStr) as Map<String, dynamic>>;
}
Future<void> _write(Map<String, dynamic> map) async {
final jsonStr = jsonEncode(map);
final file = File(filePath);
await file.writeAsString(jsonStr);
}
In my code I skipped the try-catch block and the Directory thing, that's just to make the example simpler.

Reading from text files until String max length

I'm new to coding in Dart so please bear with me. I searched up how to read files with the readAsString() function from the flutter API. It says that it will read the entire content of the file and return it as a String. However, is there some sort of String max size that it can only read? I could not find the max size of a String in Dart online. Thanks.
Here's the code in case you want a look:
import 'dart:async';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class Storage {
Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get localFile async {
final path = await localPath;
return File('$path/data.txt');
}
Future<List<String>> read() async {
try {
final file = await localFile;
String contents = await file.readAsString(); //the important part
return contents.split(";");
} catch (exception) {
return null;
}
}
void write(List data) async {
final file = await localFile;
String toWrite = "";
for (int i = 0; i < data.length; i++) {
toWrite += data.elementAt(i) + ";";
}
file.writeAsString(toWrite);
}
}
Maybe you want something like:
var myFileStream = File('path/to/file').openRead();
var firstChars = myFileStream.take(1024);
This will limit the memory part of the file to the first 1024 characters.
(I think. :)

Make PlatformFile into File in Flutter using File Picker

I am using the File Picker Plugin to choose a file from a device. The file is chosen in the datatype of a PlatformFile, but I want to send the file to Firebase Storage and I need a regular File for that. How can I convert the PlatformFile into a File so that I can send it to Firebase Storage? Here is the code:
PlatformFile pdf;
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
void _trySubmit() async {
final isValid = _formKey.currentState.validate();
if (isValid) {
_formKey.currentState.save();
final ref = FirebaseStorage.instance
.ref()
.child('article_pdf')
.child(title + '-' + author + '.pdf');
await ref.putFile(pdf).onComplete; // This throws an error saying that The argument type 'PlatformFile' can't be assigned to the parameter type 'File'
}
}
void _pickFile() async {
FilePickerResult result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['pdf'],
);
if (result != null) {
pdf = result.files.first;
}
}
Try this:
PlatformFile pdf;
final File fileForFirebase = File(pdf.path);
Happy coding! :)
If you're on a web app, you can post image files to Firestore with flutter_file_picker: (Taken from the FAQ page): https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ
// get file
final result = await FilePicker.platform.pickFiles(type: FileType.any, allowMultiple:
false);
if (result.files.first != null){
var fileBytes = result.files.first.bytes;
var fileName = result.files.first.name;
// upload file
await FirebaseStorage.instance.ref('uploads/$fileName').putData(fileBytes);
}
This works
File(platformFile.name)
Just be sure not duplicates in the file names in your logic.

compute() in flutter has no effect

I try to use compute in Flutter. Here I try to pass multiple parameters inside a Map. But the code in my function myFunction does not work. I get no errors or something else. My code seems to be ignored. Do you find an error here?
Compute function:
Map map = Map();
map['resultList'] = resultList;
map['_getImageFileFromAssets'] = _getImageFileFromAssets;
map["picturesData"] = picturesData;
map["albumID"] = albumID;
await compute(myFunction, map);
Calls the following function:
Future<bool> myFunction(map) async {
var resultList = map["resultList"];
var _getImageFileFromAssets = map["_getImageFileFromAssets"];
var picturesData = map["picturesData"];
var albumID = map["albumID"];
print("Starten");
for (var i = 0; i < resultList.length; i++) {
print(i);
File imageFile = await _getImageFileFromAssets(resultList[i]);
final appDir = await syspath.getApplicationDocumentsDirectory();
final fileName = path.basename(imageFile.path);
final savedImage =
await File(imageFile.path).copy('${appDir.path}/$fileName');
// Creating thumbnails
final thumb = image.decodeImage(await File(savedImage.path).readAsBytes());
final thumbImage = image.copyResize(thumb, width: 500);
new File('${appDir.path}/$fileName-thumb-500.jpg')
.writeAsBytes(image.encodeJpg(thumbImage));
final finalThumbImage = File('${appDir.path}/$fileName-thumb-500.jpg');
picturesData.add(Picture(
album: albumID,
path: savedImage.path,
thumbPath: finalThumbImage.path,
timestamp: Timestamp.now()));
}
return true;
}
Ok, some code - I put this in dartpad.dev:
import 'package:flutter/foundation.dart';
void main() {
Map map = Map();
compute(myFunction, map).then((result) => print(result));
}
Future<bool> myFunction(Map map) async {
print("Starten");
// fake long process
await Future.delayed(Duration(seconds: 5));
return true;
}
and get this as a console result:
Starten
true
Also: is there a reason you need the "map" parameter in your function to be dynamic? If not, I'd declare it as type Map (like I did now).

Weird behavior when adding Strings to a list

I am experiencing this weird behavior when loading assets from external storage, sometimes the path gets added to the list and most of the time the path is not added.
Here is my function, Am I missing something?
Future<List<String>> loadAssets() async {
List<String> loadedAssets = [];
loadedAssets.add('test');
try {
final Directory dir = await syspath.getExternalStorageDirectory();
dummyData.forEach((path) async {
final extPath =
path.substring(('assets/products_dummy_data/'.length));
final localPath='${dir.path}/$extPath}';
final file = File(localPath);
if (await file.exists()) {
await file.delete();
}
final data = await rootBundle.load(path);
var asUint8List =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await file.create(
recursive: true,
);
await file.writeAsBytes(asUint8List);
if (await file.exists()) {
loadedAssets.add(localPath);
}
});
} catch (e, s) {
AppHelper.appLogger.e('Error while loading assets', e, s);
}
AppHelper.appLogger.i('loadedAssets.length ${loadedAssets.length}');
return loadedAssets;
}
But I always get the length as one, for the test element added
Problem Fixed after using await Future.forEach