Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observer and computed not working with reaction #1035

Open
rsys-zeeshan-iqbal opened this issue Dec 29, 2024 · 0 comments
Open

Observer and computed not working with reaction #1035

rsys-zeeshan-iqbal opened this issue Dec 29, 2024 · 0 comments

Comments

@rsys-zeeshan-iqbal
Copy link

rsys-zeeshan-iqbal commented Dec 29, 2024

`
import 'package:mobx/mobx.dart';

      part 'auth_store.g.dart';
      class AuthStore = AuthStoreBase with _$AuthStore;
      
      abstract class AuthStoreBase with Store {
        AuthStoreBase() {
          _setupValidations();
        }
      
      
      
        late List<ReactionDisposer> _disposers;
      
      
        void _setupValidations() {
          _disposers = [
            reaction((_) => _selectedCountryIndex, setSelectedCountryIndex,
                fireImmediately: true),
          ];
        }
      
        @observable
        int _selectedCountryIndex = 0;
      
        @computed
        int get selectedCountryIndexInt => _selectedCountryIndex;
      
        @action
        void setSelectedCountryIndex(int val) {
          _selectedCountryIndex = val;
        }
      
        @computed
        String get selectedCountryCode => "+$_selectedCountryIndex";
      
        // ------------------ Disposer ------------------
        void dispose() {
          for (var e in _disposers) {
            e();
          }
        }
      }

`

When using computed flags in the UI, they do not update automatically. I either need to use setState, or the changes become visible only after scrolling or when the widget's lifecycle is updated.

here is UI code

Observer( builder: (_) { final selectedCountryCode = injector<AuthStore>().selectedCountryCode; int selectedCountryIndexInt = injector<AuthStore>().selectedCountryIndexInt; return RippleEffect( onTap: () { injector<AuthStore>() .setSelectedCountryIndex(selectedCountryIndexInt + 1); }, child: Text( "$selectedCountryCode \\ $selectedCountryIndexInt", ), ); }, ),

here is package details

build_runner: 2.4.10
mobx_codegen: 2.7.0
mobx: 2.5.0
flutter_mobx: 2.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant