isGuid method Null safety

bool? isGuid()

Checks whether the String is a valid Guid.

Example

String foo = '6d64-4396-8547-1ec1b86e081e'
bool isGuid = foo.isGuid() // returns false
String foo = '887b7923-6d64-4396-8547-1ec1b86e081e'
bool isGuid = foo.isGuid() // returns true

Implementation

bool? isGuid() {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return false;
  }
  var regex = RegExp(
      r'^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$');
  return regex.hasMatch(this!);
}