I'm trying to restore .db
file type. Since the FilePickerResult
does not support .db
extension I got forced to use type: FileType.any
and then tried to check the file picked if it has some thing like ['*.db']
or ['%.db']
but it won't work and the condition does not be a true, my code:
onTap: () async {
var databasesPath = await getDatabasesPath();
var dbPath = join(databasesPath, 'ledger.db');
print('dbPath $dbPath');
FilePickerResult? result =
await FilePicker.platform.pickFiles(
type: FileType.any,
//type: FileType.custom,
//allowedExtensions: ['db'],
);
print('result ${result?.names}');
if (result != null && result.names == ['*.db']) {
File source = File(result.files.single.path!);
await source.copy(dbPath);
print('Successfully Restored DB');
} else {
print('Some Error XXXXXXXXXXXXXXXX');
}
},
So I need some thing as Like
in SQL
to check the name like fileName.db
but in flutter with using if (result != null && result.names like ['%.db'])
.
NOTE: this method works fine without any condition, but I'm afraid that the user can picked any file which not db file so the database will get a crash.
Thank you.
CodePudding user response:
== checks for equality without considering any Regex characters. You can use endsWith instead:
result.names.length == 1 && result.names[0].endsWith('.db')
If you want to use Regex
RegExp exp = RegExp(r'.db');
if (result != null && result.names.length == 1 && exp.hasMatch(result.names[0]))
Note that an extension does not guarantee the contents of the file.