Skip to content

Instantly share code, notes, and snippets.

@valterh4ck3r
Created April 9, 2023 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valterh4ck3r/175ecc7cc3ff477c457aef40a4dc1aa9 to your computer and use it in GitHub Desktop.
Save valterh4ck3r/175ecc7cc3ff477c457aef40a4dc1aa9 to your computer and use it in GitHub Desktop.
Storage Util Flutter using Path Provider
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