Home > OS >  why the getx debounce did not execute in flutter
why the getx debounce did not execute in flutter

Time:10-31

I want to using debounce in GetX controller, but finnaly I found that the debounce code block did not executed. This is my main.dart:

import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';

import 'main_controller.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
        init: MainController(),
        builder: (controller) {
          void _onItemTapped(int index) {
            controller.updateIndex(index);
          }

          return Scaffold(
              body: Text("ddddd"),
              bottomNavigationBar: BottomNavigationBar(
                  items: [
                    BottomNavigationBarItem(icon: GestureDetector(onDoubleTap: () {}, child: Icon(Icons.home)), label: "a"),
                    BottomNavigationBarItem(icon: GestureDetector(onDoubleTap: () {}, child: Icon(Icons.subscriptions)), label: "b"),
                    BottomNavigationBarItem(icon: Icon(Icons.rss_feed), label: "c"),
                    BottomNavigationBarItem(icon: Icon(Icons.school), label: "d"),
                  ],
                  currentIndex: controller.selectIndex.value,
                  fixedColor: Theme.of(context).primaryColor,
                  onTap: _onItemTapped,
                  unselectedItemColor: Color(0xff666666),
                  type: BottomNavigationBarType.fixed));
        });
  }
}

and this is the main_controller.dart:

import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_controllers.dart';

class MainController extends GetxController {
  var selectIndex = 0.obs;
  final _count = 0.obs;

  void updateIndex(int index) {
    selectIndex.value = index;

    debounce(
      _count,
      (value) {
        print("debounce -> "   value.toString());
      },
      time: Duration(seconds: 2),
    );

    update();
  }
}

Am I missing something? what should I do to make it work.

CodePudding user response:

the workers methods in Getx should only be used in the onInit(), or the initState() as the Getx author say in this issue :

Workers must be on onInit, constructor of Controller, or initState. It doesn't matter if the data will arrive later, the moment it is declared, they will observe that variable until it is available. Quickly looking at your reproduction code, the updateUser function is called whenever a key is pressed. In this way, a new Worker is created.

so using debounce inside a specific method will result in only in errors an unexpected behaviors.

you can do this :

@override
void onInit() {
  debounce(
      _count,
      (value) {
        print("debounce -> "   value.toString());
      },
      time: Duration(seconds: 2),
    );
  super.onInit();
   }
  • Related