How to invisible widget when there is no data in firebase?

107

If you want to hide your CustomButton when there is no data you can try this:

Put your _favItemListView(data) & _addAllToFavButton(size) inside Stack and give Positioned to your CustomButton with its bottom : 1 property.

StreamBuilder

return data.isNotEmpty
       ? Stack(
          children: [
           _favItemListView(data),
           _addAllToFavButton(size),
          ],
        )
        : const Center(
           child: Text('No data found'),
        );

CustomButton:

Positioned _addAllToFavButton(Size size) => Positioned(
        width: size.width,
        bottom: 1,                        //bottom property
        child: Padding(
          padding: kSymmetricPaddingHor,
          child: CustomButton(
            label: 'Add all to my cart',
            onPressed: () {}
            },
          ),
        ),
      );
Share:
107
Mohit Ratanpara
Author by

Mohit Ratanpara

Updated on January 04, 2023

Comments

  • Mohit Ratanpara
    Mohit Ratanpara over 1 year

    i am new to flutter, i'm trying to invisible button when there is no data in Firebase.

    To get data i'm using StreamBuilder, if snapshot.data!.docs is null i want to invisible CustomButton which is outside of StreamBuilder.

    StreamBuilder:

    bool _isVisible = true; //variable
    
    @override
       Widget build(BuildContext context) {
        final size = MediaQuery.of(context).size;
        return Scaffold(
          key: _scaffoldKey,
          appBar: _appBar(context),
          body: CommonRefreshIndicator(
            child: StreamBuilder(
              stream: FirebaseFirestore.instance
                  .collection('users')
                  .doc(_currentUser!.uid)
                  .collection('favourites')
                  .snapshots(),
              builder:
                  (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return const CustomProgressIndicator();
                }
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return const CustomProgressIndicator();
                }
    
                final data = snapshot.data!.docs;
                allData = snapshot.data!.docs;
    
                if (data.isNotEmpty) {   //update based on data
                  _isVisible = true;  
                } else {
                  _isVisible = false;
                }
    
                return data.isNotEmpty
                    ? _favItemListView(data)
                    : const Center(
                        child: Text('No data found'),
                      );
              },
            ),
          ),
          bottomNavigationBar: _addAllToFavButton(size),
        );
      }
    

    CustomButton:

    Padding _addAllToFavButton(Size size) => Padding(
            padding: kSymmetricPaddingHor,
            child: Visibility(
              visible: _isVisible,
              child: CustomButton(
                label: 'Add all to my cart',
                onPressed: () {},
              ),
            ),
          );
    

    i have tried with Visibility widget and its work but whenever i'm deleting all data CustomButton is still visible, to invisivle CustomButton every time need to do hot reload.

    NOTE: setState is also not working its giving me error.

    if any one can help me! Thanks.

  • Mohit Ratanpara
    Mohit Ratanpara over 2 years
    snapshot.data!.docs is working perfectly, i want to change _isVisible to true when data.isNotEmpty and false when data.isEmpty.
  • griffins
    griffins over 2 years
    ideally you should use setstate, but wont work during widget build
  • Mohit Ratanpara
    Mohit Ratanpara over 2 years
    then what should i do to update _isVisible variable without hot reloading?
  • griffins
    griffins over 2 years
    nope, render your widget
  • griffins
    griffins over 2 years
    check comments in the code in the answer
  • Mohit Ratanpara
    Mohit Ratanpara over 2 years
    it dose not work. still need to do hot reload to invisible