Flutter:获取Firebase数据库参考子级的所有数据

时间:2018-08-29 08:31:45

标签: firebase firebase-realtime-database flutter

我有一个firebase数据库子对象,内容如下:

enter image description here

如何在颤振中检索到这个?

我当前的代码:

static Future<Query> queryUsers() async{
return FirebaseDatabase.instance
    .reference()
    .child("zoom_users")
    .orderByChild('name');
}

queryUsers().then((query){
  query.once().then((snapshot){
    //Now how to retrive this snapshot? It's value data giving a json
  });
});

2 个答案:

答案 0 :(得分:5)

要检索数据,请尝试以下操作:

db = FirebaseDatabase.instance.reference().child("zoom_users");
db.once().then((DataSnapshot snapshot){
  Map<dynamic, dynamic> values = snapshot.value;
     values.forEach((key,values) {
      print(values["name"]);
    });
 });

这里首先在子项zoom_users处添加引用,然后由于值返回data['value'],因此您可以将其分配给Map<dynamic, dynamic>,然后使用{{1} }并检索值,例如forEach

检查:

https://api.dartlang.org/stable/2.0.0/dart-core/Map/operator_get.html

Flutter: The method forEach isn't defined for the class DataSnapshot

答案 1 :(得分:1)

<script src='https://www.google.com/recaptcha/api.js' async defer></script>
<form action="/cgi-bin/mailer.pl" id="iona-enquiry" method="post" target="_self" name="iona-enquiry">
    <div class="nx-row">
        <div class="nx-c4">
            <label for="">Title</label>
            <span class="nx-select basket-wrap">
                <select name="title" id="title" class="nx-component nx-select">
                    <option value="" selected>Select</option>
                    <option value="Mr">Mr</option>
                    <option value="Mrs">Mrs</option>
                    <option value="Ms">Ms</option>
                    <option value="Miss">Miss</option>
                </select>
            </span>
        </div>
        <div class="nx-c4">
            <label for="firstname">First Name</label>
            <input type="text" value="" name="firstname" id="firstname" placeholder="First Name" required>
        </div>
        <div class="nx-c4">
            <label for="lastname">Last Name</label>
            <input type="text" value="" name="lastname" id="lastname" placeholder="Last Name" required>
        </div>
    </div>
    <div class="nx-row">
        <div class="nx-c6">
            <label for="email" class="">Email</label>
            <input type="email" name="email" id="email" placeholder="Enter email" class="nx-required" required>
        </div>
        <div class="nx-c6">
            <label for="phone" class="">Phone</label>
            <input type="tel" name="call" id="phone" placeholder="Enter Phone Number" class="nx-required" required>
        </div>
    </div>
    <div class="nx-row">
        <div class="nx-c12">
            <div class="recaptcha-center">
<script>
    jQuery('#iona-enquiry').submit(function (event) {
event.preventDefault();
grecaptcha.reset();
grecaptcha.execute();
});

   function formSubmit(response) {
     document.getElementById("iona-enquiry").submit();
   }
 </script>

<div class="g-recaptcha" data-sitekey="6LcccWoUAAAAANCtIyB8fGIwb3BO5H28xDPtsngG" data-callback="formSubmit" data-badge="inline" data-size="invisible"></div>
            </div>

            <input id='submit' type="submit" value="Advance Register" class="nx-btn nx-btn--primary nx-btn--arrowhover">

            <input type="hidden" value="thanks" name="redirect" />
            <input type="hidden" name="mailerid" value="5" />
            <input type="hidden" name="enquiry" id="enquiry" value="Iona Pre Reg" />
            <input type="hidden" name="affiliate" id="affiliate" value="[INC:displayphoneno("201")]" /> 
        </div>
    </div>
</form>

或与您已经使用的 query.once().then((snapshot){ var result = data.value.values as Iterable; for(var item in result) { print(item); } });

async