Home > Blockchain >  How do I create a map from data inside snapshot.data[index]. Flutter/Dart
How do I create a map from data inside snapshot.data[index]. Flutter/Dart

Time:07-29

I am trying to access and use values that are In my snapshot.data[index]

What I am printing using print(snapshot.data[index]) :

I/flutter (11316): Code : LE-0000000002
I/flutter (11316): Description : test_01
I/flutter (11316): Organisation Unit : 01_01_04_01_SA - Shah Alam
I/flutter (11316): Date Reported : 18/09/2020
I/flutter (11316): Status : LE110 - Pending Approval
I/flutter (11316): RunHyperlink : {classid: 25510, id: 2, mad_key: 32835}

this is the code thats feeding data into the snapshot

List<Map<String, dynamic>> incidentList = [
                    for (final json in listNode.map((x) => x.toJson()))
                      {
                        'Code': json['field'][0]['field_value'],
                        'Description': json['field'][1]['field_value'],
                        'Organisation Unit': json['field'][2]['field_value'],
                        'Date Reported': json['field'][3]['field_value'],
                        'Status': json['field'][4]['field_value'],
                        'RunHyperlink' : json['run_hyperlink']
                      }
                  ];


                  final List<String> values =  [];
                  for(final item in incidentList){
                    String groupedElement = "";
                    for(var innerItem in item.entries)
                    {
                      groupedElement  = "${innerItem.key} : ${innerItem.value}\n";
                    }
                    values.add(groupedElement);
                  }

                  await WriteCache.setListString(key: 'cache4', value: values);

and this is the future builder :

FutureBuilder(
              future: ReadCache.getStringList(key: 'cache4'),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: ListTile(
                            onTap: () {

                              print(snapshot.data[index]);
                            },
                            title: Padding(
                              padding: const EdgeInsets.only(top: 10),
                              child: Text(snapshot.data[index],
                                  style: GoogleFonts.poppins(
                                    textStyle: const TextStyle(
                                      fontWeight: FontWeight.normal,
                                      fontSize: 20,
                                    ),
                                  )),
                            ),
                            tileColor: Colors.blueGrey[200],
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)),
                          ),
                        );
                      });
                } else {
                  return const Text("No Data");
                }
              },
            ),

I want to use the "id : value" that is being displayed. I understand that need to create a map to do so but am unsure of how to go about it. Can anyone help me create this map?

The reason why I need to access and use the "id : value" is that I need to pass whatever value is in the "id" to a queryparameter.

Thanks!

CodePudding user response:

At least for a AsyncSnapshot from firestore should be able to access the id just by treating the snapshot.data as a map:

int id = snapshot.data[index]['id']

otherwise you could cast it to a Map like that:

Map snapshotData = snapshot.data[index] as Map<String, int>

CodePudding user response:

try this

   List data = snapshot.data!

you can access id by

data[index].id
  • Related