skip to Main Content

When i study they said stateless widget can change value in short static value only display, but when i try to use getx in stateless it can change the value dynamic
getx.dart file and testingGetX.dart

`import 'package:get/get.dart';

class GetX{
RxInt number = 0.obs;
void increase() {
number = number + 1;
}
}`


`import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_api_2/getX.dart';

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

@override
Widget build(BuildContext context) {
final authProvider c = Get.put(authProvider());
return Scaffold(
appBar: AppBar(),
body:Column(
  children: 
  [
    Text("value : ${c.number}"),

    Obx(()=>Text("value : ${c.number}")),
    TextButton(onPressed: () {
      c.number = c.number+1;
    }, child: Text("increase"))
  ]
),
);
}
}`

2

Answers


  1. It is not your stateless widget that is changing the state, it is the getX observer that is doing it. The state of the parent widget is always the same, only what is inside obx() is rebuilt

    Login or Signup to reply.
  2. The Obx widget in your widget tree already contains an observer that listens to the value that has been marked with "obs, then it can listen to and react to changes with the value attached to it. Think of it like its own self-proclaimed "setState", that is why it’s able to change its value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search