I usually load the sqlite data file from the assets folder on the Windows and Android platforms by writing bytedata to the file, then look for the path of the file to open it in the database. Like this for example:
sqfliteFfiInit();
factory = databaseFactoryFfi;
dbPath = join((await getTemporaryDirectory()).path, "dictionary.db");
ByteData data = await rootBundle.load('assets/dictionary.sqlite');
List<int> bytes = data.buffer.asUint8List(
data.offsetInBytes,
data.lengthInBytes,
);
await File(dbPath).writeAsBytes(bytes, flush: true);
db = await factory.openDatabase(dbPath);
List<Map> temp = await db.query('word_list');
And now how to load sqlite file data from assets folder, but in flutter web. Meanwhile, on the web platform, you cannot write files.
The libraries I used to create the database sqflite_common_ffi_web: ^0.3.1 and sqflite_common_ffi: ^2.2.0+1
Related
File('/data/user/0/com.abc/cache/full.jpg')
.writeAsBytesSync(class_image.encodeJpg(data));
I found out that Android works with this path. But in ios, I found out that it doesn't work with the error below.
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.abc/cache/full.jpg' (OS Error: No such file or directory, errno = 2)
I want to use the same folder for both. Is this possible? If not, the workaround is to manage the ios folder separately?
File('/data/user/0/com.abc/cache/full.jpg') I have observed that you have specified path manually. The path you speicified works only for android but for ios it is completely different.So instead of specifying manually use path provider and create platform specified path then use it
import 'package:path_provider/path_provider.dart';
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid ? await path.getTemporaryDirectory(): await path.getApplicationSupportDirectory(); }
The storage directory of each platform is different
You can obtain the local storage directory separately through the path_provider plugin, and then create your path.
like this
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid
? await path.getExternalStorageDirectory()
: await path.getApplicationSupportDirectory();
}
generateCsv() async {
String csvData = 'test data';
final String directory = (await getApplicationSupportDirectory()).path;
final path = "$directory/csv-${DateTime.now()}.csv";
print(path);
final File file = File(path);
await file.writeAsString(csvData);
}
in this case file generated but we cant able to open the file . it shows unsupported file format .
we also tried txt file format. it also shows same error. This is a android PDA device and when i connect to the PC the file is not showing in the PC but the interesting thing is when i copy and paste the same file in same location pasted file will open and it will shows in the PC also. My PDA android version is 7.
await file.writeAsString(text, mode: FileMode.append, flush: true);
Im trying to list all the mp3 files in the Downloads folder using
Directory dir = Directory('/storage/emulated/0/Download/');
List<FileSystemEntity> listOfAllFolderAndFiles = dir.listSync(recursive: true);
print(listOfAllFolderAndFiles);
but it only prints sub-folders and not any mp3 files.
I tried to create folder with path_provider package with this code.
final Directory directory = await getExternalStorageDirectory();
print("path1="+directory.path );// this is using path_provider
String directoryPath = directory.path + '/bozzetto_camera';
await Directory(directoryPath).create(recursive: true).then((value){
print("path="+value.toString());
});
And in console it shows path=Directory: '/storage/emulated/0/Android/data/com.example.water_delivery_app/files/bozzetto_camera'.
In file manager I could not find any folder.I tried this answer but it did not work I am using a real android device via usb. So how can create directory that user can see?
Here is the setup in side of my
pubspec.yaml
...
path_provider: ^0.4.1
assets:
- assets/
- assets/a.txt
...
I try to find way to access to the "assets" directory to read content of the text file named "a.txt" where it should read in relative way as "assets/a.txt".
Here is my code in my
main.dart:
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String fPath = '${appDocDir.parent.path}/assets/a.txt';
new File(fPath).readAsString().then((String contents) {
print("FILE:..... "+contents);
});
And here I got this error:
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/assets/a.txt' (OS Error: No such file or directory, errno = 2)
Seem like the ... EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/ is not the root directory of the app to concat with my file assets/a.txt.
How can I find the exact absolute path of my a.txt file in order to read it using File() class?
You need to use AssetBundle to read assets.
For example https://docs.flutter.io/flutter/services/AssetBundle/load.html or https://docs.flutter.io/flutter/services/AssetBundle/loadString.html
See also https://flutter.io/docs/development/ui/assets-and-images#loading-text-assets