【问题标题】:How to write a value of Encrypted Datatype in a File in Flutter如何在 Flutter 的文件中写入加密数据类型的值
【发布时间】:2020-07-24 22:20:05
【问题描述】:

我刚刚使用下面的代码生成了一个base64字符串的加密值:

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

我用这个包使用上面的代码:https://pub.dev/packages/encrypt

现在我想把这些数据保存在firebase云存储中,所以我需要把这些数据放在一个文件中,但是我不知道如何在文件中写入一个加密数据类型的值。

请帮我解决这个问题。 谢谢

【问题讨论】:

    标签: firebase file flutter encryption firebase-storage


    【解决方案1】:

    您可以将字符串写入文件:

    Future<File> writeData(string data) async {
      final file = await _localFile;
    
      // Write the file.
      return file.writeAsString('$data');
    }
    

    _localFile 派生为:

    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');
    }
    

    更多详情请见here

    将本地文件放入 Firebase 云存储是一项简单的工作。应该做以下事情:

    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");
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 2016-04-18
      • 2021-07-16
      • 1970-01-01
      • 2018-10-18
      相关资源
      最近更新 更多