Flutter getx first null value

3,786

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

Share:
3,786
4F2E4A2E
Author by

4F2E4A2E

https://www.linkedin.com/in/anofau/ https://stackoverflow.com/users/543426/4f2e4a2e https://github.com/4F2E4A2E https://github.com/orgs/anofau https://www.anofau.com

Updated on December 28, 2022

Comments

  • 4F2E4A2E
    4F2E4A2E over 1 year

    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.

  • 4F2E4A2E
    4F2E4A2E about 3 years
    Null checks can be done sure, but my question is on how to avoid this first always happening initial null value emission.