简体   繁体   English

Flutter 应用程序中 firebase 数据库上的 ValueEventListener

[英]ValueEventListener on firebase database in flutter app

I am learning android app development in flutter.我正在学习 Flutter 中的 android 应用程序开发。 I have started from flutter chat app example.我从颤振聊天应用程序示例开始。 In the Enable data syncing section , It is explained to use Firebase Database like below.启用数据同步部分,解释了如何使用 Firebase 数据库,如下所示。

final DBreference = FirebaseDatabase.instance.reference().child('messages');

Also the push() and set() methods are working fine. push()set()方法也工作正常。

When i try to listen to events on child using ValueEventListener .当我尝试使用ValueEventListener监听孩子的事件时。 The DBreference created above has no methods like addListenerForSingleValueEvent or addValueEventListener . DBreference创建的DBreference没有像addListenerForSingleValueEventaddValueEventListener这样的方法。

My main goal is to retrieve value of child as expalined in SO answers Retrieving child value -firebase- or Checking if a particular value exists in the firebase database我的主要目标是检索 SO 答案中解释的child 值 Retrieving child value -firebase-Checking if a specific value exists in the firebase database

I m getting undefined class ValueEventListener我得到undefined class ValueEventListener
if i create a new ValueEventListener , i tried by importing如果我创建一个新的ValueEventListener ,我尝试通过导入

import 'com.google.firebase.database.ValueEventListener';

I m unable to import this path as well.我也无法导入此路径。 Same error for addListenerForSingleValueEvent or addValueEventListener . addListenerForSingleValueEventaddValueEventListener相同错误。

I am using android studio 3.1我正在使用 android studio 3.1

Use

var subscription = FirebaseDatabase.instance
.reference()
.child('messages')
.onXxx
.listen((event) {
  // process event
});

where onXxx is one of其中onXxx是其中之一

  • value
  • onChildAdded
  • onChildRemoved
  • onChildChanged

To end the subscription you can use要结束订阅,您可以使用

subscription.cancel();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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