Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Method listening to an action gets called multiple times #44

@DhudumVishal

Description

@DhudumVishal

If a method is listening to an action then for first time it gets hit once, then for next time if user gets back to the same page then the method gets hit twice and on third time its 3 times and increases every time.

Below is a code to simulate the same on click of Next a action is called and on action change the user is taken to second page, but on second time the action listening method gets hit twice.

    import 'package:flutter/material.dart';
    import 'package:flutter_flux/flutter_flux.dart';

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyHome(),
        );
      }
    }
    //========
    class MyHome extends StatefulWidget {
      @override
      _MyHomeState createState() => _MyHomeState();
    }

    class _MyHomeState extends State<MyHome> with StoreWatcherMixin<MyHome>  {
      AppStore store;
      @override
      void initState() {
        store = listenToStore(appStoreToken);
        changeStatus.listen(_openSetupDialogue);
        super.initState();
      }

      void _openSetupDialogue(dynamic payload){
        if(payload == true) {
          print("method got hit");
          Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()));
        }
      }

      @override
      Widget build(BuildContext context) {
        return RaisedButton(
          child: Text("Next"),
          onPressed: (){
            changeStatus(true);
          },
        );
      }
    }
    //===========
    class SecondPage extends StatefulWidget {
      @override
      _SecondPageState createState() => _SecondPageState();
    }

    class _SecondPageState extends State<SecondPage> {
      @override
      Widget build(BuildContext context) {
        return RaisedButton(
          child: Text("back"),
          onPressed: (){Navigator.push(context, MaterialPageRoute(builder: (context) =>MyHome()),
            );
          },
        );
      }
    }
    //===========
    class AppStore extends Store {
      bool state = true;
      AppStore(){
        triggerOnAction(changeStatus, (status){
          state = status;
        });
      }
      bool get getAppStore => state;
    }
    final Action changeStatus = Action<bool>();
    final appStoreToken = StoreToken(AppStore());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions