获取List下标和元素方法

  1. s.map()s.asMap()
    map()无法获取下标;
    asMap()获取的是一个map对象, 默认以下标作为key。
  2. 例子
void main() {
    
    
  List s = ["1","2","a","b"];
  print("----------------element:");
  s.map((element){
    
    print(element);}).toList();
  print("----------------key:");
  s.asMap().keys.map((key){
    
    print(key);}).toList();
  print("----------------value:");
  s.asMap().values.map((value){
    
    print(value);}).toList();
}

输出:

----------------element:
1
2
a
b
----------------key:
0
1
2
3
----------------value:
1
2
a
b

猜你喜欢

转载自blog.csdn.net/weixin_45625639/article/details/126930557