1

Something that I didn't completely understand about the Get package is whether it is always required to put the observable variables in a controller. For example, this case works:

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

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

class App extends StatelessWidget {
  final isTrue = true.obs;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: Scaffold(
        body: Center(
          child: Obx(
            () => FlatButton(
              color: isTrue.value ? Colors.blue : Colors.red,
              child: Text('Hey'),
              onPressed: () => isTrue.value = !isTrue.value,
            ),
          ),
        ),
      ),
    );
  }
}

But would there be leaks/problems, because of this and is a GetXController necessary in this situation?

  • 1
    @Mono_Chrome Strictly speaking it's not required to put an Rx inside a GetxController, but it also doesn't serve its State Management purpose, i.e. sharing state. To share `true.obs` state outside of that single StatelessWidget, then putting it into a Controller, which lives outside the lifecycle of Widgets, would make it useful. Otherwise the observable will be destroyed/recreated along with the widget. – Baker Dec 09 '20 at 21:17
  • 1
    Re: leaks/problems, creating an `.obs` observable uses a stream and that stream would normally be closed by its containing Controller that goes out of scope. In the above sample, the closing of the stream would not happen automatically, you would have to do that yourself. – Baker Dec 09 '20 at 21:29
  • @Baker Ok, thanks for the answer. That cleared things for me :) –  Dec 11 '20 at 20:58
  • 1
    I should have used the word "reset" rather than destroyed/recreated above, as that might give the impression the underlying Observable stream would be removed/cleaned/memory freed. To my understanding, it will not be, when instantiated directly in a widget. Here's another post with more details on the cleanup that happens Observables inside GetxControllers https://stackoverflow.com/a/65117780/2301224 – Baker Dec 11 '20 at 22:48

2 Answers2

0

So as @Baker explained with his comments, the obs streams should be destroyed through the GetXControllers and they will persist in memory, if used independently in a stateless widget.

0

Yes, you can achieve using an Rx<T> observable in your UI, without using any GetxController, with the ObxValue widget which comes with the Getx package :

you can use it like this:

ObxValue<RxInt>(
          (data) => GestureDetector(
            onTap: () {
              data.value++;
            },
            child: Text("${data.value}"),
          ),
          0.obs,
        ),

as you see, you need to specify the type of the ObxValue's Rx<T>, in my case I specified RxInt, then its an initial value which is 0.obs, and in the builder, you can return a widget and manage the value of it, and it will update automatically.

Note: the ObxValue manage automatically it's lifecycle, when the widget is moved from the tree ( disposed ), also the observable will dispose / close automatically.

Gwhyyy
  • 7,554
  • 3
  • 8
  • 35