Flutter getx first null value

Issue

Hello there dear internet, and thank you for getx,

I have a question regarding getx rx flow &/ getx initial. I am new to getx, but a veteran in rxjs, where you get a value only emitted on .next(value);

My question is: how can – by all means [4] – the emission of an initial null value be avoided?
My basic understanding is that the on the UI or widget, the Obx(), Getx<Xyz>() or GetBuilder<Xyz>() will only be on value emission.

Here are some snippets regarding this question:

This specific line from [3] Text('' + _identity.value.profile.name)) always leads to null [3] first, after a few milliseconds, the response from the server gets set and all is good.
So, how to avoid that first one null value emission, ergo exception? Because that’s my expectation based on the general redux experience.

1: https://github.com/jonataslaw/getx/blob/master/lib/get_rx/src/rx_types/rx_core/rx_impl.dart#L371

2: Controller

final Rx<UserDataProfile> _userDataProfile = UserDataProfile().obs;
[...] after a few seconds milliseconds
_userDataProfile.value(xyzValue);

3: UI

class DetailScreen extends StatelessWidget {
  final logger = LoggingService().logger;

  @override
  Widget build(BuildContext context) {
    final dataService = Get.find<DataService>();
    final _identity = dataService.identity();
    return Scaffold(
      appBar: AppBar(
        title: Obx(() => Text('' + _identity.value.profile.name)),
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Get.back();
          },
        ),
      ),
    );
  }
}

3: Exception

======== Exception caught by widgets library =======================================================
The following NoSuchMethodError was thrown building Obx(dirty, state: _ObxState#b3490):
The getter 'name' was called on null.
Receiver: null
Tried calling: name

4: There is really no sense in adding null value checks, this is only – IMHO – is not the redux way of things.

Solution

The best way I could find on how to realize this, was a mix of getx and rx_widgets

enter image description here

You can grab the code here on github

Answered By – 4F2E4A2E

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *