如何在Flutter中读取和写入Firebase存储中的文本文件? [英] How do I read and write a text file from firebase storage in Flutter?

查看:50
本文介绍了如何在Flutter中读取和写入Firebase存储中的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Firebase存储器中存储我保存的图像.有什么办法可以扑朔迷离地读写Firebase存储上的在线文本文件?有什么其他办法可以存储图像吗?

I need to make a storage for my saved images in firebase storage. Is there a way I can read and write to online text file on firebase storage in flutter? Is there any alternative to make my images storage?

推荐答案

它是如此简单.让我们用示例来说明解决方案.

It's so simple. Let's explain the solution with the example.

按如下所示导入库:

import package:firebase_storage/firebase_storage.dart' as firebase_storage;

第二,通过以下方式获取Firebase参考:

Secondly, get the firebase reference by :

firebase_storage.Reference ref = firebase_storage.FirebaseStorage.instance
.ref() 
.child('playground')
.child('/put-string-example.txt');

第三,要从当前文件中获取数据:

Third, to get the data from the current file you are in :

Uint8List downloadedData  =  await ref.getData();

第四,现在您将获得Uint8List格式的值,您必须通过Utf8Codec类对该值进行解码,该类是[Utf8Codec]的默认实现,您将在String对象中获得结果:

Fourth, now you will get a value in Uint8List format, you have to decode this value by Utf8Codec class that is an instance of the default implementation of the [Utf8Codec] and you will get the result in String object:

print(utf8.decode(downloadedData)); 

现在编写起来要容易一些:首先,我们有String对象:

Now for writing it's a bit easier : Firstly, we have the String object :

String putStringText = 'Hello World';

第二,我们使用firebase ref对象写入当前文件:

Secondly, we write to the current file with firebase ref object :

ref.putString(putStringText,
metadata: firebase_storage.SettableMetadata(
contentLanguage: 'en'));

第一个参数是我们的字符串,第二个参数是元数据,它允许您使用一些额外的选项,例如contentLanguage参数.

the first argument is our string the second one is the metadata that allows you some extra options like the contentLanguage param.

我希望您觉得它有用.

这篇关于如何在Flutter中读取和写入Firebase存储中的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆