fromString static method

Binding? fromString(
  1. String? binding, {
  2. Scope? bindingscope,
})

Implementation

static Binding? fromString(String? binding, {Scope? bindingscope}) {
  if (binding == null) return null;
  try {
    String signature = binding.trim();

    // remove braces
    binding = binding.trim();
    if (binding.startsWith("{")) binding = binding.substring(1);
    if (binding.endsWith("}")) {
      binding = binding.substring(0, binding.length - 1);
    }

    String? scope;
    String? source;
    String? property;
    int? offset;

    // split binding signature int source.property
    List<String> parts = binding.split('.');

    // scoped?
    var myScope = parts[0].trim();
    if (System.currentApp != null &&
        parts.length > 1 &&
        System.currentApp!.scopeManager.hasScope(myScope)) {
      scope = myScope;
      parts.removeAt(0);
    }

    // source id
    if (parts.isNotEmpty) {
      source = parts[0].trim();
      parts.removeAt(0);
      if (source.contains(":")) {
        List<String> parts = source.split(':');
        source = parts[0].trim();
        offset = toInt(parts[1]);
      }
    }

    // property name
    if (parts.isNotEmpty) {
      property = parts[0].trim();
      parts.removeAt(0);
      if (property.contains(":")) {
        List<String> parts = property.split(':');
        property = parts[0].trim();
        offset = toInt(parts[1]);
      }
    }

    // build sub-properties (name and offset) list
    DotNotation? subproperties;
    if (parts.isNotEmpty) {
      String? name;
      for (String part in parts) {
        name = (name == null) ? part : "$name.$part";
      }
      subproperties = DotNotation.fromString(name);
    }

    // default property is "value"
    if (isNullOrEmpty(property)) property = 'value';

    // create the bindable
    if (source!.isNotEmpty) {
      return Binding(
          scope: scope,
          signature: signature,
          source: source,
          property: property!,
          dotnotation: subproperties,
          offset: offset);
    }
  } catch (e) {
    return null;
  }
  return null;
}