检查列表是否只有一个元素并返回它。
Single - 语法
List.single
Single - 示例
void main() { var lst=new List(); lst.add(12); print("The list has only one element: ${lst.single}"); }
它将产生以下输出-
The list values in reverse order: (13, 12)
它将产生以下输出-
The list has only one element: 12
如果列表中包含多个元素,则此属性将引发异常。以下示例说明了相同的内容-
void main() { var lst=new List(); lst.add(12); lst.add(10); print(lst.single); }
如果列表包含多个元素,则相同的代码将引发以下异常 -
Unhandled exception: Bad state: Too many elements #0 List.single (dart:core-patch/growable_array.dart:234) #1 main (file:///D:/Learnfk/Boolean.dart:6:13) #2 _startIsolate.<anonymous closure> (dart:isolatepatch/isolate_patch.dart:261) #3 _RawReceivePortImpl._handleMessage (dart:isolatepatch/isolate_patch.dart:148)
参考链接
https://www.learnfk.com/dart-programming/dart-programming-list-single-method.html
标签:12,lst,List,无涯,single,dart,list,Single,Dart From: https://blog.51cto.com/u_14033984/8400206