Coder Social home page Coder Social logo

Comments (5)

amondnet avatar amondnet commented on June 16, 2024 1

Hi, @wiseaidev

At the heart of MobX are observables. The reactivity occurs when there is a reaction that depends on an observable. When the observables change, the associated reactions will re-trigger automatically. Yes, automatically, without any manual wiring. The way you link a reaction to an observable is by using it (reading its value) inside the tracking function.

https://mobx.netlify.app/guides/when-does-mobx-react#rules-of-reactivity
https://mobx.netlify.app/api/observers#reactionbuilder-widget

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: ReactionBuilder(
        builder: (context) {
          return reaction(
          (_) =>  context.read<AppState>().currentPage , (v) => print('New value: $v')

            },
          );
        },
        child: Observer(
          name: "CurrentScreen",
          builder: (context) {
            switch (context.read<AppState>().currentPage) {
              case AppPage.splash:
                return const MySplashPage(title: "Splash Page");
              case AppPage.home:
                return const MySplashPage(title: "Home Page");
              case AppPage.login:
                return const Placeholder();
            }
          },
        ),
      ),
    );
  }
}

https://mobx.netlify.app/api/reaction#autorun

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: ReactionBuilder(
        builder: (context) {
          return autorun(
            (_) {
              print("A reaction has been triggered! ${context.read<AppState>().currentPage}");
            },
          );
        },
        child: Observer(
          name: "CurrentScreen",
          builder: (context) {
            switch (context.read<AppState>().currentPage) {
              case AppPage.splash:
                return const MySplashPage(title: "Splash Page");
              case AppPage.home:
                return const MySplashPage(title: "Home Page");
              case AppPage.login:
                return const Placeholder();
            }
          },
        ),
      ),
    );
  }
}
Installing build/app/outputs/flutter-apk/app-debug.apk...
Debug service listening on ws://127.0.0.1:44217/gaOdK7mROOQ=/ws
Syncing files to device sdk gphone64 x86 64...
I/x_autorun_issue( 7449): Compiler allocated 4533KB to compile void android.view.ViewRootImpl.performTraversals()
I/flutter ( 7449): A reaction has been triggered! AppPage.splash
E/SurfaceSyncer( 7449): Failed to find sync for id=0
W/Parcel  ( 7449): Expecting binder but got null!
I/flutter ( 7449): Am action has been fired!
I/flutter ( 7449): A reaction has been triggered! AppPage.home
D/EGL_emulation( 7449): app_time_stats: avg=14432.15ms min=18.67ms max=28845.63ms count=2

from mobx.dart.

wiseaidev avatar wiseaidev commented on June 16, 2024 1

Hey @amondnet,

Thanks for the response. I will try your suggestion and get back in an hour or so cause I am AFK at the moment(using gh mobile).

from mobx.dart.

amondnet avatar amondnet commented on June 16, 2024

@wiseaidev Please reopen if you have more questions.

from mobx.dart.

wiseaidev avatar wiseaidev commented on June 16, 2024

Hey @amondnet,

I have just tried both examples, but the problem still exists.

11

I think you are using an older version of flutter. My specs:

112

Therefore, I am going to reopen the issue (Oh wait! I can't do it :-(. I need karma or something).

from mobx.dart.

wiseaidev avatar wiseaidev commented on June 16, 2024

Hey @amondnet,

After lil speculation, I found that the problem exists on SDK > 2.18 (but probably on SDK >2.17 as well):
1

I tried SDK 2.16.8, and the problem is gone. So, it seems like the issue exists in the newer versions (SDK > 2.17).

from mobx.dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.