split method Null safety
- String path
Splits path
into its components using the current platform's
separator. Example:
context.split('path/to/foo'); // -> ['path', 'to', 'foo']
The path will not be normalized before splitting.
context.split('path/../foo'); // -> ['path', '..', 'foo']
If path
is absolute, the root directory will be the first element in the
array. Example:
// Unix
context.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo']
// Windows
context.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo']
context.split(r'\\server\share\path\to\foo');
// -> [r'\\server\share', 'foo', 'bar', 'baz']
// Browser
context.split('https://dart.dev/path/to/foo');
// -> ['https://dart.dev', 'path', 'to', 'foo']
Implementation
List<String> split(String path) {
final parsed = _parse(path);
// Filter out empty parts that exist due to multiple separators in a row.
parsed.parts = parsed.parts.where((part) => part.isNotEmpty).toList();
if (parsed.root != null) parsed.parts.insert(0, parsed.root!);
return parsed.parts;
}