Skip to content

Thoughts on providers in a static class for the sake of namespace? #812

@Norbert515

Description

@Norbert515

Sometimes there are a couple of providers that work together to achieve a function.

For example:

final registrationButtonEnabledProvider = Provider.autoDispose<bool>((ref) {
  var email = ref.watch(registrationEmailStateProvider).state;
  var password = ref.watch(registrationPasswordStateProvider).state;
  var passwordConfirm = ref.watch(registrationPasswordConfirmStateProvider).state;

  if(email.isEmpty || password.isEmpty || passwordConfirm.isEmpty) {
    return false;
  }

  if(!PasswordValidator.checkPassword(password).conformsToRequirements) {
    return false;
  }

  return ref.watch(registrationStateNotifierProvider).canRegister;
});

/// The [StateProvider] to set the email state
///
/// This is supposed to be only used by the [RegistrationPage]
final registrationEmailStateProvider = StateProvider.autoDispose<String>((ref) {
  return '';
});

/// The [StateProvider] to set the password state
///
/// This is supposed to be only used by the [RegistrationPage]
final registrationPasswordStateProvider = StateProvider.autoDispose<String>((ref) {
  return '';
});

/// The [StateProvider] to set the password confirm state
///
/// This is supposed to be only used by the [RegistrationPage]
final registrationPasswordConfirmStateProvider = StateProvider.autoDispose<String>((ref) {
  return '';
});

handles TextFields and whether to enable the register button. These are only meant to be used by one class.

If I create a lot of such providers, my concern is that it might convolute the global namespace and make API discovery harder when the project grows.

I was thinking of putting these in a static class, purely for the sake of namespace.

class RegistrationForm {

    static final registrationButtonEnabledProvider = Provider.autoDispose<bool>((ref) {
      var email = ref.watch(registrationEmailStateProvider).state;
      var password = ref.watch(registrationPasswordStateProvider).state;
      var passwordConfirm = ref.watch(registrationPasswordConfirmStateProvider).state;
    
      if(email.isEmpty || password.isEmpty || passwordConfirm.isEmpty) {
        return false;
      }
    
      if(!PasswordValidator.checkPassword(password).conformsToRequirements) {
        return false;
      }
    
      return ref.watch(registrationStateNotifierProvider).canRegister;
    });
    
    /// The [StateProvider] to set the email state
    ///
    /// This is supposed to be only used by the [RegistrationPage]
    static final registrationEmailStateProvider = StateProvider.autoDispose<String>((ref) {
      return '';
    });
    
    /// The [StateProvider] to set the password state
    ///
    /// This is supposed to be only used by the [RegistrationPage]
    static final registrationPasswordStateProvider = StateProvider.autoDispose<String>((ref) {
      return '';
    });
    
    /// The [StateProvider] to set the password confirm state
    ///
    /// This is supposed to be only used by the [RegistrationPage]
    static final registrationPasswordConfirmStateProvider = StateProvider.autoDispose<String>((ref) {
      return '';
    });
}

I haven't seen this done yet, and I'd love to hear opinions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions