Neil_Kerman
Neil_Kerman

Reputation: 3

How to write a value of Encrypted Datatype in a File in Flutter

I just used the following code to generate an encrypted value of a base64 string:

final encrypted = encrypter.encrypt(base64String, iv: iv);

I have used this package to use the above code: https://pub.dev/packages/encrypt

Now I want to save this data in firebase cloud storage, so I need to put this data in a file, but I have no idea how to write a value of Encrypted datatype in a file.

Please help me with this. Thanks

Upvotes: 0

Views: 221

Answers (1)

Sukhi
Sukhi

Reputation: 14185

You can write the string to a file as :

Future<File> writeData(string data) async {
  final file = await _localFile;

  // Write the file.
  return file.writeAsString('$data');
}

where the _localFile is derived as :

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
}

Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/filename.txt');
}

More details are available here.

Putting a local file to the Firebase Cloud Storage is a straight-forward job. Something like following should do :

Future<void> _uploadFile(File file, String filename) async {
    StorageReference storageReference = FirebaseStorage.instance.ref().child("files/$filename");
    final StorageUploadTask uploadTask = storageReference.putFile(file);
    final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
    final String url = (await downloadUrl.ref.getDownloadURL());
    print("URL is $url");
}

Upvotes: 1

Related Questions