user11847424
user11847424

Reputation:

Flutter Firebase Realtime Database get single data query result value null

a busy cat
(source: googleapis.com)

Path = userTokens

uid = W9aafYsF6cOPr1sMLKlnFEUcNNR2

userTokens/W9aafYsF6cOPr1sMLKlnFEUcNNR2

I want to bring the underlined area in the picture. How should I write a query with Firebase Realtime Database?

The result variable is null.

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';

class NotificationServiceTools {
  static final FirebaseDatabase firebaseDatabase = new FirebaseDatabase();

  static Future<String> getUserTokens({@required String uid}) async {
    final String result =
        (await firebaseDatabase.reference().child("userTokens/$uid").once())
            .value
            .toString();
    print("result: $result");

    return result;
  }
}

where the function is called :

print(receiver);
token = await NotificationServiceTools.getUserTokens(uid: receiver);
print("token : " + token);

log :

I/flutter ( 9447): vUpgi1QyU1UxK7tCJ5rcJqnOdaO2
I/flutter ( 9447): result : null
I/flutter ( 9447): token : null

Upvotes: 0

Views: 2212

Answers (1)

Arhaam Patvi
Arhaam Patvi

Reputation: 670

Try this,

String result;
await firebaseDatabase.instance
.reference()
.child('userTokens/$uid')
.once()
.then((snapshot){result=snapshot.value;});

Upvotes: 1

Related Questions