バリデーション
validation_helper.dart
// 英数のみ、6桁のバリデーション
class Validator {
Validator._();
static bool isValidHoge(String hoge) {
const _hogeRegExpString = r'^[0-9a-zA-Z]{6}$';
return RegExp(_hogeRegExpString, caseSensitive: true).hasMatch(hoge);
}
}
接頭辞r
を付することでraw(生)文字列として扱うことができます。(エスケープシーケンス回避のため)
参考記事
実装例
example_notifier.dart
// freezedを使用した実装例
class Hoge {
void onChangeHoge(String hoge) {
state = state.copyWith(hoge: hoge);
if (Validator.isValidHoge(hoge)) {
state = state.copyWith(isEnable: true);
} else {
state = state.copyWith(isEnable: false);
}
}
}