0

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

1 Answer 1

1

Try this,

String result;
await firebaseDatabase.instance
.reference()
.child('userTokens/$uid')
.once()
.then((snapshot){result=snapshot.value;});
Sign up to request clarification or add additional context in comments.

7 Comments

@TheNobleBrain what is the name of the root? is it 'userTokens/$uid' for sure?
@TheNobleBrain and are you giving the correct uid as an input to getUserTokens()?
@TheNobleBrain so your root node is 'userTokens/vUpgi1QyU1UxK7tCJ5rcJqnOdaO2'? you've hidden that part in the screenshot and it's important
@TheNobleBrain why is 'vUpgi1QyU1UxK7tCJ5rcJqnOdaO2' being passed to getUserTokens()? and you still haven't cleared up the root, what is above userTokens in the screenshot?
@TheNobleBrain Yeah I was wondering why that particular uid was being passed even though it's not available in your database, glad you figured it out :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.