0

I'm working on a project with Firebase (Realtime database). In this project I will have a main screen with will have several buttons according to the user. The Buttons info are going to be stored inside the realtime database. This is basically a Home Automation project.

This is how my db looks: Db

The quantity, means how many buttons does that user have. button1 and button2 have the button characteristics. So what I'm attempting to do is.

When the user logs in. I have a Streambuilder that will check if the quantity has data. If I has if will run inside a For loop which will create the buttons in the user screen.

I having problem getting the specific values from the database, for example, getting the quantity and storing into a variable in the main screen.
This is how I'm attempting to get the quantity (I will use this code for getting other values too, later on) but it isn't working:

Future<int> receive_quantity() async{
    final FirebaseUser user = await _auth.currentUser();
    var snapshot = databaseReference.child(user.uid+"/buttons"+"/quantity").once();
    var result;
    await snapshot.then((value) => result = value);
    print(result);
    return result;
  }

Error that I get:

_TypeError (type 'DataSnapshot' is not a subtype of type 'FutureOr<int>')

My StreamBuilder:

body: StreamBuilder(
        stream: _auth.getButtonQuantity(),
        initialData: 0,
        builder: (context, snapshot) {
          if (snapshot.hasError || snapshot.hasError){
            return Container(color: Colors.red);
          }
          if (!snapshot.hasData || !snapshot.hasData){
            return Center(child: CircularProgressIndicator());
          }
          if (snapshot.hasData || snapshot.hasData){
            return GridView.count(
              padding: EdgeInsets.all(15),
              crossAxisSpacing: 20.0,
              mainAxisSpacing: 20.0,
              crossAxisCount: 3,
              children: [
                for (int i = 0; i < buttonquant; i++){
                  Button(),
                },
                GestureDetector(
                  onTap: () async{
                    _auth.receive_quantity();
                  },
                    child: Container(
                    color: Colors.black,
                    width: 150,
                    height: 150,
                    child: Icon(Icons.add, color: Colors.white,),
                    
                  ),
                ),
              ],
            );
          }
        }
      ),

My Button:

class Button extends StatelessWidget {
  const Button({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        
      },
      child: Container(
        width: 150,
        height: 150,
        decoration: BoxDecoration(
          color: Colors.black,
          borderRadius: BorderRadius.circular(15)
        ),
        child: Stack(
          children: [
            Positioned(
              top: 10,
              left: 10,
              child: Icon(
                Icons.lightbulb,
                size: 35,
                color: Colors.white,
              ),
            ),
            Positioned(
              top: 95,
              left: 15,
              child: Text("Televisao", style: TextStyle(color: Colors.white),),
            ),
          ],
        ),
      ),
    );
  }
}```

1 Answer 1

1

What you need to do is you need to get the value of the snapshot not using it directly:

Future<int> receive_quantity() async{
    final FirebaseUser user = await _auth.currentUser();
    var snapshot = await databaseReference.child(user.uid+"/buttons"+"/quantity").once();
    var result = snapshot.value; //get the value here
    print(result);
    return result;
  }

This is how you get the value in general:

databaseReference.once().then((DataSnapshot snapshot) {
    print('Data : ${snapshot.value}');
  });
Sign up to request clarification or add additional context in comments.

2 Comments

I have just updated the code as you told me, I'm using a button to call the function. I'm calling it like this: onTap: () async{ int vv = await _auth.receive_quantity(); print(vv); }, But I'm getting 0 as response
So you don't get the error anymore. Check the value in your database as it might have changed to 0. It also depends on the user id you are using. You are may be accessing another document.

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.