0

I managed to get one data from Firebase Realtime Database. This time I want to turn it into JSON format and get two data at least. Below is the code

my Database Reference `

DatabaseReference ref =
        FirebaseDatabase.instance.ref().child('UsersData/$userDataUID/test/int');

my code `

StreamBuilder(
              stream: ref.onValue,
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  debugPrint(snapshot.data?.snapshot.value.toString());   
                }
                return const CircularProgressIndicator();
              })),

my Firebase console enter image description here

I believe I have to convert it into Maps? I have no idea how to begin with

1
  • you can get the map with child('UsersData/$userDataUID/test') Commented Nov 14, 2022 at 17:54

1 Answer 1

1

Try with

DatabaseReference ref =
        FirebaseDatabase.instance.ref().child('UsersData/$userDataUID/test');

This will return a map with those fields.

And use on widget like

 if (snapshot.hasData) {
     final map =  snapshot.data?.snapshot.value as Map?;
     return Text("int: ${map?["int"]}  voltage: ${map?["voltage"]}"); }

Find more about getting data

Sign up to request clarification or add additional context in comments.

8 Comments

I tried your method but it shows the error on map?["int"] that says 'The operator '[]' isn't defined for the type 'DatabaseEvent'. Try defining the operator '[]'.'
try snapshot.data as Map?;
new error : type 'DatabaseEvent' is not a subtype of type 'Map<dynamic, dynamic>?' in type cast
oh it should be snapshot.data?.snapshot.value as Map? i think
it works now after I put ''' snapshot.data?.snapshot.value as Map? '''. Thank you very much!
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.