asMap function

Map? asMap(
  1. Object? o
)

Returns o as Map. Converts it if needed.

Implementation

Map? asMap(Object? o) {
  if (o == null) return null;
  if (o is Map) return o;

  var m = {};

  if (o is List) {
    var sz = o.length;

    for (var i = 0; i < sz; i += 2) {
      Object key = o[i];
      Object? val = o[i + 1];
      m[key] = val;
    }
  } else {
    throw StateError("Can't handle type: $o");
  }

  return m;
}