简体   繁体   中英

ValueEventListener on firebase database in flutter app

I am learning android app development in flutter. I have started from flutter chat app example. In the Enable data syncing section , It is explained to use Firebase Database like below.

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

Also the push() and set() methods are working fine.

When i try to listen to events on child using ValueEventListener . The DBreference created above has no methods like addListenerForSingleValueEvent or addValueEventListener .

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

I m getting undefined class ValueEventListener
if i create a new ValueEventListener , i tried by importing

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

I m unable to import this path as well. Same error for addListenerForSingleValueEvent or addValueEventListener .

I am using android studio 3.1

Use

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

where onXxx is one of

  • value
  • onChildAdded
  • onChildRemoved
  • onChildChanged

To end the subscription you can use

subscription.cancel();

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