interpolation 2.1.2 copy "interpolation: ^2.1.2" to clipboard
interpolation: ^2.1.2 copied to clipboard

A Dart package to handle dynamic String & Json interpolation.

example/interpolation_example.dart

import 'package:interpolation/interpolation.dart';

void main() {
  var interpolation = Interpolation();
  var str =
      "Hi, my name is '{name}'. I'm {age}. I am {education.degree} {education.profession}.";

  print(interpolation.eval(str, {
    'name': 'David',
    'age': 29,
    'education': {'degree': 'M.B.B.S', 'profession': 'Doctor'}
  }));
  // output: Hi, my name is 'David'. I'm 29. I am M.B.B.S Doctor.

  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}'
    }
  };

  // traverse the object
  print(interpolation.traverse(obj, 'b'));
  // output: 10
  print(interpolation.traverse(obj, 'c.e'));
  // output: Hello {c.d}
  print(interpolation.traverse(obj, 'c.g')); // not present
  // output: (empty string)
  print(interpolation.traverse(obj, 'c.g', true)); // not present but keepAlive
  // output: {c.g}

  // resolve the 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}}
  print(obj);
  // original object is not changed
  // output: {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}}}
}
9
likes
140
pub points
80%
popularity

Publisher

verified publishermarganam.com

A Dart package to handle dynamic String & Json interpolation.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on interpolation