-1

I am trying to build a simple application with Getx. I am actually trying out the package trying to understand more how it works and I am stuck at a place. Please help me. There are two screens. 1st screen has two text fields and using the controller I am saving data into a data model and showing it into the ListView builder widget.
I am passing that data using arguments when the user clicks on the individual list tile.
The second screen shows that data in two different Text widgets. I want to decrement the count of one of the Text widgets using the controller. How do I do it?

SCREEN 1 PASSING DATA FROM HERE

Obx(() => ListView.builder(
                  shrinkWrap: true,
                  itemCount: controller.tasbees.length,
                  itemBuilder: (context, index) {
                    List<Tasbeeh> tasbees = controller.tasbees
                        .map((value) => Tasbeeh(
                            tasbeehName: value.tasbeehName,
                            tasbeehCount: value.tasbeehCount))
                        .toList();
                    return Card(
                      elevation: 3.0,
                      child: ListTile(
                        leading: Text(
                          tasbees[index].tasbeehCount,
                          style: const TextStyle(
                              fontSize: Sizes.dimen_18,
                              fontWeight: FontWeight.w500),
                        ),
                        title: Text(
                          tasbees[index].tasbeehName,
                          style: const TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: Sizes.dimen_20),
                        ),
                        trailing: IconButton(
                          icon: const Icon(Icons.arrow_forward_ios_sharp),
                          onPressed: () {
                            var data = {
                              "name": tasbees[index].tasbeehName,
                              "count": tasbees[index].tasbeehCount
                            };
                            Get.toNamed(Routes.home, arguments: data);
                          },
                        ),
                      ),
                    );
                  },
                ))

SCREEN 2 RECEIVING DATA HERE.

Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have done ${data["name"]} tashbeeh this many times:',
            ),
            vertical30,
            //NEED OBX HERE FROM CONTROLLER,
            Text('Count: ${data["count"]}'),
            vertical50,
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                FloatingActionButton.extended(
                  onPressed: () {},
                  tooltip: 'reset',
                  label: const Text('Reset'),
                ),
                FloatingActionButton.extended(
                  onPressed: () {
                    //TODO: UPDATE COUNT HERE WITH DECREMENT METHOD
                  },
                  backgroundColor: AppColors.orangeWeb,
                  tooltip: 'decrement',
                  label: const Text('Tap Here'),
                ),
              ],
            ),
          ],
        ),
      ),  

MY CONTROLLER CLASS where I want my passed arguments from Screen 1 to make it observable and convert String type to int so that I can decrement it, similar to counter app.

class HomeController extends GetxController {
  final FirebaseAuthentication _authentication = FirebaseAuthentication();

  /*Map<String, String> params = Get.arguments;
  String myTest;
 */
  var count = Get.arguments["count"];

  void decrement() {
    //TODO: Decrement Tasbeeh Count here
  }

  void onLogOut() async {
    await _authentication.logOut();
  }
}  
  1. How do I make arguments observable? (only 1 argument is needed).
  2. How to convert that argument into int and decrement it?
sulli110
  • 145
  • 2
  • 16

1 Answers1

0

I received an answer from somewhere else so I am also posting it here. Pass the arguments from Screen 1 like this:

onPressed: () {
                            var count = int.parse(tasbees[index].tasbeehCount);
                            var arguments = <String, dynamic>{
                              "name": tasbees[index].tasbeehName,
                              "count": count
                            };
                            Get.toNamed(Routes.home, arguments: arguments);
                          },  

HomeController will look like this:

var count = 0.obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    Map<String, dynamic> args = Get.arguments;
    count = RxInt(args["count"]);
  }

  void resetCount() {
    Map<String, dynamic> args = Get.arguments;
    if (count.value <= 0 || count.value < args["count"]) {
      count.value = args["count"];
    }
  }

  void decrement() {
    //TODO: Decrement Tasbeeh Count here
    count.value--;
    if (count.value <= 0) {
      count.value = 0;
      Get.defaultDialog(
        radius: Sizes.dimen_10,
        contentPadding: const EdgeInsets.all(Sizes.dimen_20),
        title: 'Tasbeeh Done',
        middleText: 'You have finished doing Tasbeeh',
        textConfirm: 'Okay',
        confirm: OutlinedButton.icon(
          onPressed: () => Get.back(),
          icon: const Icon(
            Icons.check,
            color: Colors.blue,
          ),
          label: const Text(
            'Okay',
            style: TextStyle(color: Colors.blue),
          ),
        ),
      );
    }
  }
sulli110
  • 145
  • 2
  • 16