Bearbeiten von „Dart“

Zur Navigation springen Zur Suche springen

Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird bei Bearbeitungen öffentlich sichtbar. Melde dich an oder erstelle ein Benutzerkonto, damit Bearbeitungen deinem Benutzernamen zugeordnet werden.

Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.

Aktuelle Version Dein Text
Zeile 8: Zeile 8:


= Statements =
= Statements =
<syntaxhighlight lang=dart>for (var ix in list) { print(ix); }
<source lang=dart>for (var ix in list) { print(ix); }
for (var entry in list.entries) { print(entry.key + entry.value); }
for (var entry in list.entries) { print(entry.key + entry.value); }
for (int ix = 0; ix < 10; ix++) {}
for (int ix = 0; ix < 10; ix++) {}
Zeile 21: Zeile 21:
}
}
assert(str.isEmpty(), "String muss leer sein"); // nur im Debug aktiv.
assert(str.isEmpty(), "String muss leer sein"); // nur im Debug aktiv.
</syntaxhighlight>
</source>
== Exception ==
== Exception ==
<syntaxhighlight lang=dart>
<source lang=dart>
try {
try {
   breedMoreLlamas();
   breedMoreLlamas();
Zeile 41: Zeile 41:
   MyException(this.message);
   MyException(this.message);
}
}
</syntaxhighlight>
</source>


= Class =
= Class =
<syntaxhighlight lang=dart>abstract class BaseLogger {
<source lang=dart>abstract class BaseLogger {
   int _errors = 0;
   int _errors = 0;
   int _level;
   int _level;
Zeile 69: Zeile 69:
   const logger2 = BaseLogger.lastInstance();
   const logger2 = BaseLogger.lastInstance();
   var isSilent = logger.isSilent;
   var isSilent = logger.isSilent;
</syntaxhighlight>
</source>


== Named constructors ==
== Named constructors ==
Zeile 113: Zeile 113:


=== Enum ===
=== Enum ===
<syntaxhighlight lang=dart>enum DataType { bool, int, string, customType } // Schlüsselwörter erlaubt!
<source lang=dart>enum DataType { bool, int, string, customType } // Schlüsselwörter erlaubt!
DataType.values.forEach((v) => print('value: $v, index: ${v.index}'));
DataType.values.forEach((v) => print('value: $v, index: ${v.index}'));
</syntaxhighlight>
</source>


== Interface ==
== Interface ==
* Jede Klasse kann Interface sein. Dann muss jede Methode überschrieben werden
* Jede Klasse kann Interface sein. Dann muss jede Methode überschrieben werden
<syntaxhighlight lang=dart>
<source lang=dart>
class D implements A, B, C {
class D implements A, B, C {
   @override
   @override
Zeile 126: Zeile 126:
   }
   }
}
}
</syntaxhighlight>
</source>


== Generator ==
== Generator ==
<syntaxhighlight lang=dart>Iterable<int> naturalsTo(int n) sync* {
<source lang=dart>Iterable<int> naturalsTo(int n) sync* {
   int k = 0;
   int k = 0;
   while (k < n) yield k++;
   while (k < n) yield k++;
}
}
</syntaxhighlight>
</source>
* ... und mit Rekursion:
* ... und mit Rekursion:
<syntaxhighlight lang=dart>Iterable<int> naturalsDownFrom(int n) sync* {
<source lang=dart>Iterable<int> naturalsDownFrom(int n) sync* {
   if (n > 0) {
   if (n > 0) {
     yield n;
     yield n;
Zeile 141: Zeile 141:
   }
   }
}
}
</syntaxhighlight>
</source>


= Typen =
= Typen =
* Casting:
* Casting:
<syntaxhighlight lang=dart>
<source lang=dart>
final x = y as String;
final x = y as String;
</syntaxhighlight>
</source>
== typedef ==
== typedef ==
Definiert eine Methodensignatur oder einen Typ-Alias:
Definiert eine Methodensignatur oder einen Typ-Alias:
<syntaxhighlight lang=dart>
<source lang=dart>
typedef IntList = List<int>;
typedef IntList = List<int>;
typedef Compare<T> = int Function(T a, T b);
typedef Compare<T> = int Function(T a, T b);
Zeile 158: Zeile 158:
   if (validator(input)) doIt();
   if (validator(input)) doIt();
}
}
</syntaxhighlight>
</source>


== Map ==
== Map ==
<syntaxhighlight lang=dart>final map = <String, int>{ 'John': 1, 'Eve': 2 };
<source lang=dart>final map = <String, int>{ 'John': 1, 'Eve': 2 };
final knowsEve = map.containsKey('Eve') && map.containsValue(2);
final knowsEve = map.containsKey('Eve') && map.containsValue(2);
map.remove('John');
map.remove('John');
Zeile 168: Zeile 168:
map.forEach((k, v) { print('{ key: $k, value: $v }'); });
map.forEach((k, v) { print('{ key: $k, value: $v }'); });
map.entries.forEach((e) {  print('{ key: ${e.key}, value: ${e.value} }'); });
map.entries.forEach((e) {  print('{ key: ${e.key}, value: ${e.value} }'); });
</syntaxhighlight>
</source>


== List ==
== List ==
* Indizes: start: inklusiv end: exklusiv
* Indizes: start: inklusiv end: exklusiv
<syntaxhighlight lang=dart>final names = <String>['adam', 'bob', 'charly', 'eve'];
<source lang=dart>final names = <String>['adam', 'bob', 'charly', 'eve'];
final names2 = [...names, 'judy'];
final names2 = [...names, 'judy'];
names.add('fred'); names.insert(3, 'chris');
names.add('fred'); names.insert(3, 'chris');
Zeile 192: Zeile 192:
// fold(), reduce(), shuffle(), removeWhere(),
// fold(), reduce(), shuffle(), removeWhere(),
// foreach(), join(), contains()
// foreach(), join(), contains()
</syntaxhighlight>
</source>


== DateTime ==
== DateTime ==
<syntaxhighlight lang=dart>
<source lang=dart>
// Für DateFormat
// Für DateFormat
import 'package:intl/intl.dart';
import 'package:intl/intl.dart';
Zeile 212: Zeile 212:
var formatter = DateFormat('yyyy.MM.dd HH:mm:ss dayOfWeek: E');
var formatter = DateFormat('yyyy.MM.dd HH:mm:ss dayOfWeek: E');
String formatted = formatter.format(now);
String formatted = formatter.format(now);
</syntaxhighlight>
</source>


== Set ==
== Set ==
<syntaxhighlight lang="dart">
<source lang="dart">
var foundKeys = <String>{};
var foundKeys = <String>{};
if (! foundKeys.contains('x')){
if (! foundKeys.contains('x')){
Zeile 222: Zeile 222:
// Iterable unique machen:
// Iterable unique machen:
words.split(' ').toSet().toList();
words.split(' ').toSet().toList();
</syntaxhighlight>
</source>


== RegExpr ==
== RegExpr ==
Zeile 259: Zeile 259:


= Json =
= Json =
<syntaxhighlight lang=dart>class Photo {
<source lang=dart>class Photo {
   final int id;
   final int id;
   final String title;
   final String title;
Zeile 274: Zeile 274:
   }
   }
}
}
</syntaxhighlight>
</source>


= Besonderheiten =
= Besonderheiten =
* entweder optionale Positionsparameter oder optionale Namensparameter, nicht beide.
* entweder optionale Positionsparameter oder optionale Namensparameter, nicht beide.
<syntaxhighlight lang=dart>String substr(String str, int pos, [int length, bool uppercase]){ ... }
<source lang=dart>String substr(String str, int pos, [int length, bool uppercase]){ ... }
String substr2(String str, int pos, {int length, bool uppercase, Logger logger}){ ... }
String substr2(String str, int pos, {int length, bool uppercase, Logger logger}){ ... }
x = substr('Hi world', 3, 2);
x = substr('Hi world', 3, 2);
Zeile 288: Zeile 288:
x ~/ 5 // Ganzzahlige Division
x ~/ 5 // Ganzzahlige Division
logger?.log() // Aufruf von log nur, wenn logger!=null
logger?.log() // Aufruf von log nur, wenn logger!=null
</syntaxhighlight>
</source>


== Reflection, Runtime-Info ==
== Reflection, Runtime-Info ==
<syntaxhighlight lang=dart>if (a.runtimeType == int || a.runtimeType == String){...}
<source lang=dart>if (a.runtimeType == int || a.runtimeType == String){...}
if (a is String || or a is Map || a is! List){...}
if (a is String || or a is Map || a is! List){...}


</syntaxhighlight>
</source>


== UnitTest ==
== UnitTest ==

Bitte kopiere keine Webseiten, die nicht deine eigenen sind, benutze keine urheberrechtlich geschützten Werke ohne Erlaubnis des Urhebers!
Du gibst uns hiermit deine Zusage, dass du den Text selbst verfasst hast, dass der Text Allgemeingut (public domain) ist, oder dass der Urheber seine Zustimmung gegeben hat. Falls dieser Text bereits woanders veröffentlicht wurde, weise bitte auf der Diskussionsseite darauf hin. Bitte beachte, dass alle Info-Theke-Beiträge automatisch unter der „Gemeinfreiheit“ stehen (siehe Info-Theke:Urheberrechte für Einzelheiten). Falls du nicht möchtest, dass deine Arbeit hier von anderen verändert und verbreitet wird, dann klicke nicht auf „Seite speichern“.

Abbrechen Bearbeitungshilfe (wird in einem neuen Fenster geöffnet)