Dart 映射子级的有条件成员访问?

Dart 映射子级的有条件成员访问?,dart,Dart,假设您试图访问地图中嵌套较深的子对象,但您无法期望他们的父母在那里。例如: Map awesomeMap = { "this":{ "is":{ "sometimes":"not here" } } } Map notAwesomeMap = { "this":{ "haha":{ "we":"switched" } } } 当我访问notAweso

假设您试图访问地图中嵌套较深的子对象,但您无法期望他们的父母在那里。例如:

Map awesomeMap = {
    "this":{
        "is":{
            "sometimes":"not here"
        }
    }
}

Map notAwesomeMap = {
    "this":{
        "haha":{
            "we":"switched"
        }
    }
}
当我访问
notAwesomeMap['this']['is']['times']
时,它将返回一个错误,因为
['this']['is']
null
,并且您无法查找
null
的值
['times']

这很好,但我希望能够使用条件成员访问操作符

notAwesomeMap['this']?['is']?['times'].

但这不起作用

除了用try块包装所有内容外,有没有处理这些情况的好方法

编辑:我试着玩这个,但我没有发现任何真正有启发性的东西,但也许这给了某人一个想法

void main() {
  Map nestedMap = {
    'this':{
      'is':{
        'sometimes':'here'
      }
    }
  };

  final mapResult = nestedMap['this'];
  print(mapResult); //returns {is: {sometimes: here}}

  final nullResult = nestedMap['this']['is an'];
  print(nullResult); // returns null


  final nullifiedResult = nullify(nestedMap['this']['is an']['error']);
  print(nullifiedResult); // returns error, but is this possible another way?

  final errorResult = nestedMap['this']['is an']['error'];
  print(errorResult); // returns error
}



nullify(object){
  try {
    final result = object;
    return result;
  }
  catch (e) {
    return null;
  }
}
一种方法是

final result = (((nestedMap ?? const {})['this'] ?? const {})['is an'] ?? const {})['error'];

另请参见

您可以编写一个简单的函数来帮助执行您想要的操作:

R lookup<R, K>(Map<K, dynamic> map, Iterable<K> keys, [R defaultTo]);
实施示例:

void main(){
var nestedMap={
“这个”:{
“是”:{
“有时”:“这里”
}
}
};
打印(查找(nestedMap,['this']);
打印(查找(nestedMap,['this','is']);
打印(查找(nestedMap,['this','is','有时']);
打印(查找(nestedMap,['this','is','error']);
//对空的纾困:
打印(查找(nestedMap,['error'],'Default Value');
}
R查找(映射,可编辑键,[R defaultTo]){
动态电流=map;
用于(最终键入键){
如果(当前为地图){
电流=电流[键];
}否则{
归还违约金;
}
}
返回电流为R;
}

我喜欢@matanlurey的方法,但做了两个改变:

  • 默认值删除为
    ,因为您仍然可以使用可读性更好的
    ??
  • 燕子型铸造错误
  • R查找(映射、可编辑键){
    动态电流=map;
    用于(最终键入键){
    如果(当前为地图){
    电流=电流[键];
    }
    }
    试一试{
    返回电流为R;
    }捕获(e){
    //无所事事
    }
    }
    
    用法相似

    String someValue=lookup(nestedMap,['some','value'])默认值';
    
    是要求使用特殊语法的问题。该功能似乎已在路线图上:
    final result = lookup(inputMap, ['this', 'is', 'something']);
    
    void main() {
      var nestedMap = {
        'this':{
          'is':{
            'sometimes':'here'
          }
        }
      };
    
      print(lookup(nestedMap, ['this']));
      print(lookup(nestedMap, ['this', 'is']));
      print(lookup(nestedMap, ['this', 'is', 'sometimes']));
      print(lookup(nestedMap, ['this', 'is', 'error']));
    
      // Bail out on null:
      print(lookup(nestedMap, ['error'], 'Default Value'));
    }
    
    R lookup<R, K>(Map<K, dynamic> map, Iterable<K> keys, [R defaultTo]) {
      dynamic current = map;
      for (final key in keys) {
        if (current is Map<K, dynamic>) {
          current = current[key];
        } else {
          return defaultTo;
        }
      }
      return current as R;
    }