Interpolation class
The Interpolation class to handle String & Json interpolation.
Create an instance of Interpolation
var interpolation = Interpolation();
// or, with custom option
var interpolationCustom = Interpolation(
option: InterpolationOption(prefix: r'$(', suffix: ')', subKeyPointer: '_'));
Now, a string with placeholder would look like
var str = "Hi, my name is '{name}'. I'm {age}. I am {education.degree} {education.profession}.";
// or, with custom option
var strCustom = "Hi, my name is '$(name)'. I'm $(age). I am $(education_degree) $(education_profession).";
and, the appropriate value collection for it may be
var value = {
'name': 'David',
'age': 29,
'education': {
'degree': 'M.B.B.S',
'profession': 'Doctor'
}
}
Now, just use eval to get the interpolated string
print(interpolation.eval(str, value));
// or, with custom option
print(interpolationCustom.eval(strCustom, value));
// output: Hi, my name is 'David'. I'm 29. I am M.B.B.S Doctor.
Similarly a json equivalent dart object may be
var obj = {
'a': 'a',
'b': 10,
'c': {
'd': 'd',
'e': 'Hello {c.d}',
'f': 'Hi "{a}", am I deep enough, or need to show "{c.e}" with {b}'
}
};
which contains its own placeholder value in itself. Let's resolve this to get the interpolated object
print(interpolation.resolve(obj));
// output: {a: a, b: 10, c: {d: d, e: Hello d, f: Hi "a", am I deep enough, or need to show "Hello d" with 10}}
Constructors
- Interpolation({InterpolationOption? option})
-
Default factory constructor for Interpolation.
factory
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
-
eval(
String str, Map values, [bool keepAlive = false]) → String -
Evaluate (substitute)
str
with providedvalues
. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
resolve(
Map obj, [bool keepAlive = false]) → Map -
Resolve (substitute all placeholders) inside a json equivalent
dart
obj
with its own values. -
toString(
) → String -
A string representation of this object.
inherited
-
traverse(
Map? obj, String key, [bool keepAlive = false]) → String -
Get the value of the
key
from a complexobj
.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited