TextReplacer class
A utility class for performing conditional and mapped text replacements.
Supports:
- Conditional expressions
{CONDITION ? "trueText" : "falseText"} - Mapped expressions
{KEY: {a:valueA, b:valueB}} - Placeholder substitution from a provided
Map<String, Object?>
Example:
final input = '{IS_LOADING ? "Loading..." : "{COUNT > 1 ? "COUNT items" : "{COUNT == 1 ? "One item" : "No items"}"}"}';
TextReplacer.replace(input, {'COUNT': 1, 'IS_LOADING': true}); // Loading...
TextReplacer.replace(input, {'COUNT': 0, 'IS_LOADING': false}); // No items
TextReplacer.replace(input, {'COUNT': 1, 'IS_LOADING': false}); // One item
TextReplacer.replace(input, {'COUNT': 2, 'IS_LOADING': false}); // 2 items
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
replace(
String input, Map< String, Object?> args) → String -
final inp = '{IS_LOADING ? "Loading..." : "{COUNT > 1 ? "COUNT items" : "{COUNT == 1 ? "One item" : "No items"}"}"}'; TextReplacer.replace(inp, {'COUNT': 1, 'IS_LOADING': true}); // Loading... TextReplacer.replace(inp, {'COUNT': 0, 'IS_LOADING': false}); // No items TextReplacer.replace(inp, {'COUNT': 1, 'IS_LOADING': false}); // One item TextReplacer.replace(inp, {'COUNT': 2, 'IS_LOADING': false}); // 2 items final inp1 = "There {NUMBER > 1 ? \"are NUMBER items\" : \"is an item\"} in stock"; TextReplacer.replace(inp1, {'NUMBER': 1}); // There is an item in stock TextReplacer.replace(inp1, {'NUMBER': 2}); // There are 2 items in stock final inp2 = "Status: {STATUS == active ? \"activated!\" : \"canceled!\"}"; TextReplacer.replace(inp2, {'STATUS': "active"}); // Status: activated! TextReplacer.replace(inp2, {'STATUS': "inactive"}); // Status: canceled! final inp3 = "Status: {IS_ACTIVATED ? \"activated!\" : \"inactivated!\"}"; TextReplacer.replace(inp3, {'IS_ACTIVATED': true}); // Status: activated! TextReplacer.replace(inp3, {'IS_ACTIVATED': false}); // Status: inactivated! final inp4 = "Last seen: {TIME: {a:now, b:3 min ago}}"; TextReplacer.replace(inp4, {"TIME": "a"}); // Last seen: now TextReplacer.replace(inp4, {"TIME": "b"}); // Last seen: 3 min ago