archive
archive copied to clipboard
Invalid zip file created with ZipFileEncoder
I tried to create zip file with following code:
ZipFileEncoder encoder = ZipFileEncoder(); encoder.create(join(documentsDirectory.path, 'product.zip'));
encoder.addFile(File('${documentsDirectory.path}/TestDB.db'));
List<Supplier> _supplierList = await DBProvider.db.getAllSupplier(); _supplierList.forEach((Supplier _supplier) { encoder.addFile(File(_supplier.photo)); _generateItemArchive(encoder, _supplier.id); }); encoder.close();//here exception raised.
void _generateItemArchive(ZipFileEncoder encoder, int id) async { List<Item> _itemList = await DBProvider.db.getAllItem(id); _itemList.forEach((Item _item) { encoder.addFile(File(_item.photo)); }); What I am missing? The zip file created with above code is invalid. Can you please guid?
I'm not entirely sure what the problem might be. The one bit that I would investigate is _generateItemArchive is marked as async, and the full function doesn't seem to be shown here, so is there something actually asynchronous about it? Maybe use an await when calling it?
I tried _generateItemArchive to call as await but got an error as following: The await expression can only be used in an asynchronous function. Try marking the function body with either 'async' or 'async*'.dart(await_in_wrong_context) - the method already as aync. _generateItemArchive is async as it uses Sqflite package for SQLite.
Problem is not _generateItemArchive, I tried with only encoder.addFile(File('${documentsDirectory.path}/TestDB.db')); with same result as invalid zip file for test purpose.
Ok, throws out that theory :-). You said there's an exception thrown from encoder.close(). What exception is that?
I think I found a clue, let me dig more and provide you details. You are right about the problem is in the async method. As my app data flow has changed a little bit I have to create one more level for an item to get its photo.