Home > Blockchain >  How not to show the saved product?
How not to show the saved product?

Time:12-21

I save the product id to local storage when the "add to cart" button is clicked. However, after that, I don't want this button to show up anymore.

I decided to do it this way - run my array with id through the loop, and if there is an id that is equal to the id of the open product, then do not show the button.

During the test, I realized that only the first added id is not shown. Why? More. That is, I click on add to cart for cards with id number 1,2,3. I go to the first product - there is no button, but in the rest there is

Tell me how to fix this?

Here is my code -

for(int i = 0; i < MyApp.ids.length; i  ) {
            if(MyApp.ids[i] == state.details.data.id.toString()) {
              return Scaffold(

saved id -

InkWell(
                              onTap: () async {
                                final prefs = await SharedPreferences.getInstance();
                                savedIds(state.details.data.id.toString());
                                await prefs.setStringList('id', MyApp.ids);
                                final kek = prefs.getStringList('id');
                                print(kek);
                              },
                              child: Container(
                                padding: EdgeInsets.all(8),
                                width: 100,
                                decoration: BoxDecoration(
                                    color: Colors.transparent,
                                    borderRadius: BorderRadius.circular(13),
                                    border: Border.all(width: 1, color: Colors.black38)
                                ),
                                child: Text('Выполнено!', textAlign: TextAlign.center,),
                              ),
                            )

CodePudding user response:

You should see this:

                Visibility(
                            visible: visible,
                            child: ElevatedButton(onPressed: () {
                          final prefs = await SharedPreferences.getInstance();
                          savedIds(state.details.data.id.toString());
                          await prefs.setStringList('id', MyApp.ids).whenComplete(() {
                            visible = !visible;
                          });
                          final kek = prefs.getStringList('id');
                          print(kek);
                        },child: Text('add to cart'),))
                                

Try whenComplete method for this.

visible ? ElevatedButton()

  • Related