6

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

Hummingbird
  • 647
  • 8
  • 27

1 Answers1

7

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();
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567