Created
April 9, 2023 03:47
-
-
Save valterh4ck3r/175ecc7cc3ff477c457aef40a4dc1aa9 to your computer and use it in GitHub Desktop.
Storage Util Flutter using Path Provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:encrypt/encrypt.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
import 'dart:async'; | |
import 'dart:io'; | |
class StorageUtil { | |
static Future<String> getLocalPath() async { | |
var directory = await getApplicationDocumentsDirectory(); | |
return directory.path; | |
} | |
static Future<String> readFile(Future<File> localFile) async { | |
try { | |
final file = await localFile; | |
String content = await file.readAsString(); | |
return content; | |
} catch (e) { | |
return ''; | |
} | |
} | |
static Future<File> writeFile(String object , Future<File> localFile) async { | |
final file = await localFile; | |
return file.writeAsString('$object', mode: FileMode.append); | |
} | |
static Future<File> cleanFile(Future<File> localFile) async { | |
final file = await localFile; | |
return file.writeAsString(''); | |
} | |
static Encrypted encrypt(String data){ | |
final key = Key.fromUtf8('my 32 length key................'); | |
final iv = IV.fromLength(16); | |
final encrypter = Encrypter(AES(key)); | |
final encrypted = encrypter.encrypt(data, iv: iv); | |
return encrypted; | |
} | |
static String decrypt(Encrypted data){ | |
final key = Key.fromUtf8('my 32 length key................'); | |
final iv = IV.fromLength(16); | |
final encrypter = Encrypter(AES(key)); | |
return encrypter.decrypt(data, iv: iv); | |
} | |
static String toBase64(File file){ | |
List<int> imageBytes = file.readAsBytesSync(); | |
return base64Encode(imageBytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment