Wrap this whole block in `dateListener != null` and then just do a simple `if (wasSelected) { .. } else { .. }` inside.