tryParse static method
Parses 'ltr' / 'rtl' (case-insensitive, whitespace-trimmed) into a
TextWritingDirection; returns null for anything else — never throws.
Matching rules, pinned so config/JSON round-trips stay stable:
- Case-insensitive:
'LTR','Ltr','rTl'all resolve. - Only LEADING/TRAILING whitespace is stripped (via
String.trim()), so inner-space tokens like'l t r'are rejected — there is no substring or partial matching ('ltrx','xrtl','ltr;'→ null). String.trim()strips Unicode whitespace too (U+00A0 NBSP, thin space, ideographic space), so' ltr 'parses; but it does NOT strip zero-width chars (U+200B, U+FEFF BOM), so those leading a token → null.- The Unicode direction MARKS themselves (LRM U+200E, RLM U+200F) are not the literal tokens and so → null.
- No normalization or case-folding beyond ASCII lowercasing: full-width
'ltr', accented'ĺtr', and emoji never match. - Lowercasing is invariant/ASCII-only here (
L/T/R), so the result is independent of the active locale — no Turkish-I dotless-i pitfall.
Total function: returns null (never throws) for null, empty, whitespace-only, numeric strings, control characters, and arbitrarily long junk — safe to call on any untrusted input.
Example:
TextDirectionParseUtils.tryParse(' LTR '); // TextWritingDirection.ltr
TextDirectionParseUtils.tryParse('auto'); // null
TextDirectionParseUtils.tryParse(null); // null
Audited: 2026-06-12 11:26 EDT
Implementation
static TextWritingDirection? tryParse(String? value) =>
// Total by construction: a null input maps straight to null, and the
// switch's exhaustive default turns every unmatched token into null,
// so no path can throw regardless of the string's content.
switch (value == null ? null : _trim(value).toLowerCase()) {
'rtl' => TextWritingDirection.rtl,
'ltr' => TextWritingDirection.ltr,
_ => null,
};