I'm trying to save a list of documents in approveList[] if approve variable in firestore is true and populate that list in a grid view later. But a problem occurs when I switch between navBar pages. each time I navigate to a new page, all data in approve list is being duplicated again in approveList, Like if approve have [a,b], after changing to another page it has [a,b,a,b]
Here is the code where approve list is getting data:
List<Object> approveList = [];
Future getRestaurantsList() async {
var doc;
var collection = await
FirebaseFirestore.instance.collection('Restaurants').where('status', isEqualTo:
"Approved");
var querySnapshots =
await collection.get().then((value) => value.docs.forEach((element) {
print(element.reference);
approveList.add(element.reference.id);
}));
}
this where im using getRestaurantList
Widget build(BuildContext context) {
return Scaffold(
body: getBody(),
bottomNavigationBar: _buildBottomBar(),
);
}
Widget getBody() {
List<Widget> pages = [
Scaffold(
appBar: AppBar(
bottomOpacity: 0.0,
elevation: 0.0,
backgroundColor: Colors.white,
automaticallyImplyLeading: false,
title: Text("Approved Restaurants", style: TextStyle(color: Colors.deepOrange),),
leading: IconButton(onPressed: () { Navigator.pop(context); }, icon: Icon(Icons.arrow_back_ios,color: Colors.black,),),
),
body: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expanded(
child: FutureBuilder(
future: getRestaurantsList(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: approveList.length,
itemBuilder: (context, index) {
return ListTile(
title: ApprovedRestaurants(documentId: approveList[index] as String, Index: index ,),
);
});
}))
],
),
),
),
Scaffold(
appBar: AppBar(
bottomOpacity: 0.0,
elevation: 0.0,
backgroundColor: Colors.white,
automaticallyImplyLeading: false,
title: Text("Pending Approvals", style: TextStyle(color: Colors.deepOrange),),
leading: IconButton(onPressed: () { Navigator.pop(context); }, icon: Icon(Icons.arrow_back_ios,color: Colors.black,),),
),
body: Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(),
Expanded(
child: FutureBuilder(
future: getRequestList(),
builder: (context, snapshot) {
return ListView.builder(
itemCount: requestList.length,
itemBuilder: (context, index) {
return ListTile(
title: RequestRestaurants(documentId: requestList[index] as String,),
);
});
}))
],
),
),
),
// Container(
// alignment: Alignment.center,
// child: Text("Messages",style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),
// ),
// Container(
// alignment: Alignment.center,
// child: Text("Settings",style: TextStyle(fontSize: 25,fontWeight: FontWeight.bold),),
// ),
];
return IndexedStack(
index: _currentIndex,
children: pages,
);
}
there is one more function right after getRestaurantList which is approveList() which is doing the same functioning
getRestaurantsListandgetRestaurantsList()