flutter_smart_select icon indicating copy to clipboard operation
flutter_smart_select copied to clipboard

A S2SingleChanges<String> was used after being disposed.

Open sonOfwidgets opened this issue 4 years ago • 8 comments

when the value is set somewhere outside SmartSelect widget using setState, the S2Tile doesnt get redrawn. Forcing to redraw the widget by adding 'key' would result in error like mentioned in the subject.

          SmartSelect<String>.single(
            key: UniqueKey(),
            title: 'Category',
            value: categorySelected,```

sonOfwidgets avatar Oct 19 '20 01:10 sonOfwidgets

@sonOfwidgets I tried this and it worked for me:

GlobalKey<S2State> collectionKey = GlobalKey<S2State>();
 SmartSelect<String>.single(
                    key: collectionKey,
                    title: AppLocalizations.of(context)
                        .translate('page_product_filter_collection'),
                    placeholder: 'Choose one',
                    value: widget.filters['collection_id'],
                    onChange: (state) => setState(() => widget
                        .filters['collection_id'] = state.value.toString()),
                    choiceItems: [
                      for (var productCollection
                          in state.productCollections ?? [])
                        S2Choice(
                          value: productCollection.id.toString(),
                          title: productCollection.displayName,
                        ),
                    ],
                    modalFilter: true,
                    tileBuilder: (context, stateTile) {
                      return S2Tile.fromState(
                        stateTile,
                        isTwoLine: true,
                        isLoading: state.requestProductCollections,
                        padding: EdgeInsets.only(
                            left: 0, right: 0, top: 0, bottom: 0),
                        trailing: widget.filters['collection_id'] != null
                            ? IconButton(
                                icon: Icon(Icons.close),
                                onPressed: () {
                                  setState(() {
                                    widget.filters['collection_id'] = null;
                                    collectionKey = GlobalKey<S2State>();
                                  });
                                },
                              )
                            : IconButton(
                                icon: Icon(Icons.keyboard_arrow_right),
                                onPressed: null),
                      );
                    },
                  );

lekhang2512 avatar Nov 20 '20 07:11 lekhang2512

@sonOfwidgets I tried this and it worked for me:

GlobalKey<S2State> collectionKey = GlobalKey<S2State>();
 SmartSelect<String>.single(
                    key: collectionKey,
                    title: AppLocalizations.of(context)
                        .translate('page_product_filter_collection'),
                    placeholder: 'Choose one',
                    value: widget.filters['collection_id'],
                    onChange: (state) => setState(() => widget
                        .filters['collection_id'] = state.value.toString()),
                    choiceItems: [
                      for (var productCollection
                          in state.productCollections ?? [])
                        S2Choice(
                          value: productCollection.id.toString(),
                          title: productCollection.displayName,
                        ),
                    ],
                    modalFilter: true,
                    tileBuilder: (context, stateTile) {
                      return S2Tile.fromState(
                        stateTile,
                        isTwoLine: true,
                        isLoading: state.requestProductCollections,
                        padding: EdgeInsets.only(
                            left: 0, right: 0, top: 0, bottom: 0),
                        trailing: widget.filters['collection_id'] != null
                            ? IconButton(
                                icon: Icon(Icons.close),
                                onPressed: () {
                                  setState(() {
                                    widget.filters['collection_id'] = null;
                                    collectionKey = GlobalKey<S2State>();
                                  });
                                },
                              )
                            : IconButton(
                                icon: Icon(Icons.keyboard_arrow_right),
                                onPressed: null),
                      );
                    },
                  );

It does not work for me

yunior123 avatar Dec 05 '20 22:12 yunior123

Anyone know how to fix this ?

gamelaster avatar Feb 01 '21 18:02 gamelaster

Could you remove UniqueKey and try again. I have faced same issue and solved with remove UniqueKey.

ayberkcal avatar Jul 05 '21 22:07 ayberkcal

i tried every solution above, still no luck.

if setState is called outside SmartSelect, then error goes away.

But the main goal is to 'setState' as soon as user select other option/chip.

RudyLie1988 avatar Jul 17 '21 15:07 RudyLie1988

Anyone know how to fix this ? What can i do to reset the selection?

hsynpsdmr avatar Oct 12 '21 11:10 hsynpsdmr

Anyone know how to fix this ? What can i do to reset the selection?

I solved this problem by interfering with the library. This works for me. I took the set code of the library and wrapped it with the GetStorage block. In this way, I would be able to trigger my code from the outside. (You can use something else to trigger it. It will probably work.) The main reason I'm doing this here is that setState cannot interfere with the library from outside. I added the set initial default value part.

1

Here, I have run my code by triggering it under any method.

2

Here, my process only works for a single selection. For multi-select, you need to add the set code under the multi-select initState in the same way. Otherwise it only works for single selection. Good job everyone, hope it works for you.

hsynpsdmr avatar Oct 29 '21 22:10 hsynpsdmr

I released https://pub.dev/packages/flutter_awesome_select with fixed null safety. Could you check that everything works for you?

vasilich6107 avatar Nov 12 '21 23:11 vasilich6107

Hi everyone,

I'm really sorry for not maintaining the smart_select package in a long time. It's been a great project, but it's become too difficult for me to maintain, especially since I've been going through a tough time for the past few years.

In its place, I've released a new package called choice. The combination to smart_select and chips_choice with cleaner, more flexible, and composable API for creating inline or prompted choice widgets with single or multiple selection.

I hope you'll check out choice. I think you'll find it to be a great replacement for smart_select.

Thanks for your understanding.

davigmacode avatar Aug 26 '23 11:08 davigmacode