简体   繁体   中英

Get field and values from firebase flutter

How will i be able to retrieve the field and field values from firebase?

i want to retrieve "total payment" and its value and store it in array. Below is my code for getting the field values but not the field names.

Future _getDataFromDatabase() async {
    await FirebaseFirestore.instance.collection("payments").doc(FirebaseAuth.instance.currentUser!.uid).get().then((snapshot)async{
      if(snapshot.exists){
        setState((){
          totalPayments = snapshot.data()!["total payment"].toString();
          balance = snapshot.data()!["remaining balance"].toString();
          print(totalPayments);     
        });
      }
    });
  }

在此处输入图像描述

snapshot is of type Map<String, dynamic> so if I understand your question correctly you are asking how to cast a map into a list with its keys preserved as string values.

you can get a list of a key-value pairs using something like

IterableZip([snapshot.keys, snapshot.values])

and then you can flatten it into a list using any approach but the shortest would be using expand so the final code would be:

import 'package:collection/collection.dart';
//... after that you can use
IterableZip([snapshot.keys, snapshot.values]).expand((i) => i).toList()

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