Programmation Dart - Méthode List.single

Vérifie si la liste n'a qu'un seul élément et le renvoie.

Syntaxe

List.single

Exemple

void main() { 
   var lst = new List(); 
   lst.add(12);
   print("The list has only one element: ${lst.single}"); 
}

Il produira ce qui suit output -

The list values in reverse order: (13, 12)

Il produira ce qui suit output -

The list has only one element: 12

Cette propriété jette un exceptionsi la liste contient plus d'un élément. L'exemple suivant illustre la même chose -

void main() { 
   var lst = new List(); 
   lst.add(12); 
   lst.add(10); 
   print(lst.single); 
}

Si la liste a plus d'un élément, le même code lancera ce qui suit exception -

Unhandled exception: 
Bad state: Too many elements 
#0 List.single (dart:core-patch/growable_array.dart:234) 
#1 main (file:///D:/Demos/Boolean.dart:6:13) 
#2 _startIsolate.<anonymous closure> (dart:isolatepatch/isolate_patch.dart:261) 
#3 _RawReceivePortImpl._handleMessage (dart:isolatepatch/isolate_patch.dart:148)