flutter - Flutter:如何将打印输出转换为列表?
问题描述
我有以下代码,按下时会返回一个字符串,我想将其转换为 List 变量以在下拉菜单中使用。
我一直在使用打印功能来检查我是否从 Cloud Firestore 获取了正确的数据,但我不确定如何使用 Firebase.Instance 来生成 List 变量。
代码:
child: RaisedButton(
child: Text('Submit Survey'),
onPressed: () async {
await Firestore.instance
.collection('MySurveys')
.document('FirestoreTestSurvey')
.get()
.then((DocumentSnapshot document) {
print("${document.data['a_ge_vi']}");
});
输出:
I/flutter (13548): [Conservatives, Labour, Liberal Democrats, SNP, Plaid Cymru, Brexit Party, Other, I would not vote]
我将如何使用 Firestore.instance 来创建一个列表变量,然后当应用程序加载时我可以在下拉菜单中使用它。
任何帮助将不胜感激!
解决方案
你可以这样处理
onPressed: () async {
await Firestore.instance
.collection('MySurveys')
.document('FirestoreTestSurvey')
.get()
.then((DocumentSnapshot document) {
List<String> stringList= document.data['a_ge_vi'];
print(stringList);
});
更新放里面initState()
@override
void initState() {
super.initState();
await Firestore.instance
.collection('MySurveys')
.document('FirestoreTestSurvey')
.get()
.then((DocumentSnapshot document) {
List<String> stringList=
document.data['a_ge_vi'];
print(stringList);
}
推荐阅读
- php - Laravel 仅存储 DB 表中数组的最后一个元素(foreach 循环)
- entity-framework - 在实体框架中获取实体及其相关数据时的主键与外键
- javascript - 如何在 django 中将模型数据传递给 Javascript?
- python - 如何读取 json 响应的不同键值对?
- laravel - 作曲家更新后应用作曲家不存在
- typescript - 判别联合上的条件类型
- vue.js - 在 Vue 中 - this.$data.foo 与 this.foo 有什么区别?
- bixby - 通过 Voice for Bixby 捕获街道地址
- ios - Xcode:如何为指向两个不同团队帐户(企业和公司)的两个不同目标选择两个不同的应用程序组
- javascript - 如何编写一个将电话号码作为字符串接收并验证它是否是美国电话号码的函数?