reverseSlash method Null safety

String? reverseSlash(
  1. int direction
)

Reverses slash by providing direction,

0 = / -> \\

1 = \\-> /

Example

String foo1 = 'C:/Documents/user/test';
String revFoo1 = foo1.reverseSlash(0); // returns 'C:\Documents\user\test'

String foo2 = 'C:\\Documents\\user\\test';
String revFoo2 = foo1.reverseSlash(1); // returns 'C:/Documents/user/test'

Implementation

String? reverseSlash(int direction) {
  if (this == null) {
    return null;
  }
  if (this!.isEmpty) {
    return this;
  }
  switch (direction) {
    case 0:
      return this!.replaceAll('/', '\\');
    case 1:
      return this!.replaceAll('\\', '/');
    default:
      return this;
  }
}