简体   繁体   中英

How to retrieve the value of key "folderName" from the following JSON data in Flutter?

Here's what I'm trying:

Future<void> _chooseFolder() async {
    final snapshot = await ref.get();
    if (snapshot.exists) {
      print(snapshot.value);
      var v = Map<String, dynamic>.from(snapshot.value as Map);
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
                title: Text('Choose folder'),
                content: Container(
                  height: 300.0,
                  width: 300.0,
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: snapshot.children.length,
                    itemBuilder: (BuildContext context, int index) {
                      return ListTile(
                        title: v['folderName'],
                      );
                    },
                  ),
                ));
          });
    } else {
      print('No data available.');
    }
  }

Here's the data I am getting:

{-NKKEkzjOLaHqdmZSQQN: {folderName: test 8}, -NKKEb1eMBmOyhVdQA8b: {folderName: test 6}, -NKKGCxwDJngEjJG5bDQ: {folderName: test 9}, -NKKEfUoygYzFWH-2PbU: {folderName: test 7}}

Here's how the database looks:

unique_user_id
   -folders
      - -NKKEkzjOLaHqdmZSQQN
           -folderName: "test"

The key -NKKEkzjOLaHqdmZSQQN & others are auto-generated.

How can I retrieve the value of folderName from this data?

try following way

for (var child in snapshot.children) {
  var folderName = child.value['folderName'];
  print(folderName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM