Dart 扩展:List 的动态索引

问题描述 投票:0回答:1

你能给我一些关于在 Dart 中为 List 对象创建动态索引扩展的建议吗

以下是我的意思:

final List<String> myList = ['one', 'two', 'three'];

// by default, get an value at [index] belike:
myList[1];

// but what I want to implement belike:
myList.1;
myList.0;
myList.2;
myList.5; // ==> will throw exception

也许它们看起来很愚蠢,但对我来说,它是如此酷和奇特的编码风格

感谢您的帮助和建议

list dart extension-methods
1个回答
0
投票
import 'package:amra/amra.dart' as amra;

void main(List<String> arguments) {

MyList._0;
MyList._1;
MyList._2;



  }
class MyList{
static List<String> myList = ['one', 'two', 'three'];
static String _0(){
return MyList.myList[0];
}
static String _1(){
 return MyList.myList[1];
}
static String _2(){
 return MyList.myList[2];
}




  }
© www.soinside.com 2019 - 2024. All rights reserved.