首页 > 解决方案 > 使用 StreamBuilder 中的数据更新文本小部件

问题描述

我试图通过使用 StreamBuilder 中的数据来更新文本小部件,但我的文本小部件位于 StreamBuilder 之外。

我想更新 -> 文本(${store_count})。store_count 在 StreamBuilder 中更新。

我曾尝试在 streambuilder 中使用 setState 但颤振不允许它。

总结代码:

 Center(child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(children: [Text("Depodaki Ürünler: ${store_count}",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,decoration: TextDecoration.underline)),
              Expanded(
                  child: StreamBuilder<QuerySnapshot>(
                      stream: FirebaseFirestore.instance.collection('Products').snapshots(),
                      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                                        
                        List<DocumentSnapshot> listofDocument = snapshot.data!.docs;
                        List<String> liststore = [] ;
                        for(int i = 0; i<listofDocument.length;i++){
                          if(listofDocument[i]['status'] == "Depoda"){
                            liststore.add(listofDocument[i]['barcodNumber']);
                            store_count = liststore.length; //here
                          }

                        }

这是我的代码:

class _ProductListScreenState extends State<ProductListScreen>   {


   int store_count = 0;
   int wash_count = 0;
   int overlok_count = 0;

   @override
   void initState() {
    super.initState();
  }


@override
Widget build(BuildContext context) {

return MaterialApp(
    home: DefaultTabController(
      length:3,
    child: Scaffold(
      appBar: AppBar(
        title: Center(child: Text("Ürün Listesi       ",style:TextStyle(color: Colors.black))),
        bottom: const TabBar(
            tabs: [
              Tab(icon: Icon(Icons.move_to_inbox_rounded,color: Colors.black)),
              Tab(icon: Icon(Icons.wash,color: Colors.black)),
              Tab(icon: Icon(Icons.cut_outlined,color: Colors.black)),
            ]
        ),
        backgroundColor:  Colors.blue,
        leading: GestureDetector(onTap: () => {Navigator.pop(context)},
            child: Icon(Icons.arrow_back,color: Colors.black)),
      ),
      body: TabBarView(
        children: [

          Center(child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(children: [Text("Depodaki Ürünler: ${store_count}",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,decoration: TextDecoration.underline)),
              Expanded(
                  child: StreamBuilder<QuerySnapshot>(
                      stream: FirebaseFirestore.instance.collection('Products').snapshots(),
                      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {



                        if (snapshot.hasError) {

                          return Text('Bir hata meydana geldi.');

                        }

                        if (snapshot.connectionState == ConnectionState.waiting) {

                          return Text("Yükleniyor");
                        }



                        List<DocumentSnapshot> listofDocument = snapshot.data!.docs;
                        List<String> liststore = [] ;
                        for(int i = 0; i<listofDocument.length;i++){
                          if(listofDocument[i]['status'] == "Depoda"){
                            liststore.add(listofDocument[i]['barcodNumber']);
                            store_count = liststore.length;
                          }

                        }





                        return ListView.builder(
                            itemCount: listofDocument.length,
                            itemBuilder: (context, index){
                              // print("------------${_productController.text.toString()}----:${listofDocument[index]['name']}");
                              if(listofDocument[index]['status'] == "Depoda") {
                                return new Card(
                                  color: Colors.white60,
                                  elevation: 4,
                                  shape:RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(15)),
                                  child: ListTile(
                                    title: Text(listofDocument[index]['barcodNumber']),
                                    subtitle: Text(listofDocument[index]['status']),
                                    onTap: (){Navigator.push(context, MaterialPageRoute(builder: (context)=> ProductInfoScreen(id: listofDocument[index].id,dropStatus:listofDocument[index]["status"] ,) ));},

                                  ),
                                );
                              }else{
                                return SizedBox();
                              }
                            }
                        );}







                  ))

            ],),
          ),)
          ,//--------------------------------
          Center(child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(children: [Text("Yıkamadaki Ürünler ${wash_count}",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,decoration: TextDecoration.underline)),
              Expanded(
                  child: StreamBuilder<QuerySnapshot>(
                      stream: FirebaseFirestore.instance.collection('Products').snapshots(),
                      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {


                        if (snapshot.hasError) {

                          return Text('Bir hata meydana geldi.');

                        }

                        if (snapshot.connectionState == ConnectionState.waiting) {

                          return Text("Yükleniyor");
                        }



                        List<DocumentSnapshot> listofDocument = snapshot.data!.docs;
                        List<String> listwash = [] ;
                        for(int i = 0; i<listofDocument.length;i++){
                          if(listofDocument[i]['status'] == "Yıkamada"){
                            listwash.add(listofDocument[i]['barcodNumber']);
                            wash_count = listwash.length;

                          }

                        }










                        return ListView.builder(
                            itemCount: listofDocument.length,
                            itemBuilder: (context, index){
                              // print("------------${_productController.text.toString()}----:${listofDocument[index]['name']}");
                              if(listofDocument[index]['status'] == "Yıkamada") {



                                return new Card(
                                  color: Colors.white60,
                                  elevation: 4,
                                  shape:RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(15)),
                                  child: ListTile(
                                    title: Text(listofDocument[index]['barcodNumber']),
                                    subtitle: Text(listofDocument[index]['status']),
                                    onTap: (){Navigator.push(context, MaterialPageRoute(builder: (context)=> ProductInfoScreen(id: listofDocument[index].id,dropStatus:listofDocument[index]["status"] ,) ));},

                                  ),
                                );
                              }else{
                                return SizedBox();
                              }
                            }
                        );}







                  ))

            ],),
          ),)
          ,//--------------------------------


          Center(child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(children: [Text("Overlok işlemindeki Ürünler: ${overlok_count}",style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,decoration: TextDecoration.underline)),
              Expanded(
                  child: StreamBuilder<QuerySnapshot>(
                      stream: FirebaseFirestore.instance.collection('Products').snapshots(),
                      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {

                        if (snapshot.hasError) {

                          return Text('Bir hata meydana geldi.');

                        }

                        if (snapshot.connectionState == ConnectionState.waiting) {

                          return Text("Yükleniyor");
                        }



                        List<DocumentSnapshot> listofDocument = snapshot.data!.docs;
                        List<String> listoverlok = [] ;
                        for(int i = 0; i<listofDocument.length;i++){
                          if(listofDocument[i]['status'] == "Overlok"){
                            listoverlok.add(listofDocument[i]['barcodNumber']);
                            overlok_count = listoverlok.length;
                          }

                        }



                        return ListView.builder(
                            itemCount: listofDocument.length,
                            itemBuilder: (context, index){
                              // print("------------${_productController.text.toString()}----:${listofDocument[index]['name']}");
                              if(listofDocument[index]['status'] == "Overlok") {
                                return new Card(
                                  color: Colors.white60,
                                  elevation: 4,
                                  shape:RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(15)),
                                  child: ListTile(
                                    title: Text(listofDocument[index]['barcodNumber']),
                                    subtitle: Text(listofDocument[index]['status']),
                                    onTap: (){Navigator.push(context, MaterialPageRoute(builder: (context)=> ProductInfoScreen(id: listofDocument[index].id,dropStatus:listofDocument[index]["status"] ,) ));},

                                  ),
                                );
                              }else{
                                return SizedBox();
                              }
                            }
                        );}







                  ))

            ],),
          ),)
          ,
        ],

    ),






  )
    )
);
}







}

标签: flutterstream-builder

解决方案


推荐阅读