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

Time:12-31

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?

CodePudding user response:

try following way

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

  • Related