Home > database >  How to define stream variable in getx package?
How to define stream variable in getx package?

Time:09-09

I want use rx variable in getx pacakge, how can I define it?

import 'package:get/get.dart';
    
    class InitReportController extends GetxController {
      int count = 0;
    
    }

CodePudding user response:

You must use this structure.

import 'package:get/get.dart';

class InitReportController extends GetxController {
  Rx<count> count = 0.obs;
  void countChange(int i) => count.value = i;
}

and when you want to change a count variable, call countChange like this:

countChange = 2

and when you want to use the count variable, you have to use this structure:

count.value;

CodePudding user response:

There is a detailed example described in the GetX documentation. You can use .obs with Obx() to achieve stream variable.

Snippet from GetX documentation.

class Controller extends GetxController{
  var count = 0.obs;

  void increment() => count  ;
}

And in your widget use like this

    final _controller = Get.find<Controller>();
    
      @override
      Widget build(context){
         // Access the updated count variable
         return Scaffold(body: Center(child: Text("${c.count}")),
floatingActionButton:
          FloatingActionButton(child: Icon(Icons.add), onPressed: c.increment));
      }

Details can be found in the link below. https://pub.dev/packages/get#counter-app-with-getx

  • Related