首页 > 解决方案 > 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 来创建一个列表变量,然后当应用程序加载时我可以在下拉菜单中使用它。

任何帮助将不胜感激!

标签: flutterdartgoogle-cloud-firestore

解决方案


你可以这样处理

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

推荐阅读