0

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

1
  • Can you include more details where and how you are using getRestaurantsList and getRestaurantsList() Commented Sep 27, 2022 at 11:20

1 Answer 1

1

You can clear the list before adding, this will solve the issue

List<Object> approveList = [];
Future getRestaurantsList() async {
  approveList.clear();

But while you are using FutureBuilder, create a future variable on state class like

late final future  = getRestaurantsList();

And use it on

child: FutureBuilder(
     future: future,

It would be better to return list from future instead of storing on another variable.

More about using FutureBuilder

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

2 Comments

It worked, thanks, but the part where getRestaurantsList() is stored in the variable as you suggested, didn't work.
I prefer creating different context/widget for page, The second approach mostly used on stateful widget to avoid unnecessary calling the API

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.