DEV Community

Cover image for Dart Typing 💫 🌌 ✨
Gülsen Keskin
Gülsen Keskin

Posted on

5 4

Dart Typing 💫 🌌 ✨

Dart, tip kontrolünü iki farklı zamanda gerçekleştirir:
• Kod derlendiğinde (kod yeniden yüklenir/veya önceden derlenir).
• Kod çalıştırıldığında (runtime).

Static Tipler

int: Tamsayılar
double: Ondalık sayılar(çift hassasiyet)
bool: Boolean true ya da false
String : Immutable (değişmez) string
StringBuffer: Mutable(değişken) string
RegExp: Düzenli ifadeler
List, Map, Set: Koleksiyon class'ları
DateTime
Duration: Bir zaman aralığı
Uri: Identifier
Error: Hata bilgisi

Dynamic Tipler (aka Untyped)

Tür bilgisi olmayan değişkenleri var veya dynamic anahtar kelimelerini kullanarak tanımlayabilirsiniz.

İkisinin arasında ince bir fark vardır:

void main() {
print (multiplyMethod1(2,4));
print (multiplyMethod2(2,4));
}
dynamic multiplyMethod1(int a, int b){
return a * b;
}
var multiplyMethod2(int a, int b){
return a * b;
}
Enter fullscreen mode Exit fullscreen mode

Bu kodu derlemeye çalıştığınızda aşağıdaki hatayı alırsınız:

Error compiling to JavaScript: main.dart:10:1: Error: The return type can't be 'var'. var multiplyMethod2(int a, int b){ ^^^ Error: Compilation failed.
Enter fullscreen mode Exit fullscreen mode

Bunun nedeni methodların bir tür döndürmesi gerekmesi ve var döndürememesidir. Bir tür belirtmeniz gerekir.

Tip Çıkarımı (Type Inference)
Örnek 1:

void main() {
dynamic x = 1;
if (x is int){
print('integer');
}
}

Enter fullscreen mode Exit fullscreen mode

Output:

integer
Enter fullscreen mode Exit fullscreen mode

Örnek 2:

void main() {
dynamic x = 'test';
if (x is String){
print('String');
}
x += 1;
}
Enter fullscreen mode Exit fullscreen mode

Output:

String Uncaught exception: TypeError: 1: type 'JSInt' is not a subtype of type 'String'
Enter fullscreen mode Exit fullscreen mode

Tip Eşleştirme

Dart, kullanıcıların 'is' anahtar sözcüğünü kullanarak tipleri kontrol etmesine olanak tanır.

main(){
printType(23);
printType('mark');
}
printType(dynamic d){
if (d is int){
print ('Its an Integer');
}
if (d is String){
print ('Its a String');
}
}
Enter fullscreen mode Exit fullscreen mode

Output:

Its an Integer
Its a String
Enter fullscreen mode Exit fullscreen mode

Tip Bilgileri

Çalışma zamanında runtimeType özelliğini kullanarak bir nesnenin tipine erişebilirsiniz.

void main() {
var v1 = 10;
print(v1.runtimeType);
var v2 = 'hello';
print(v2.runtimeType);
}
Enter fullscreen mode Exit fullscreen mode

Çıktı:

int
String
Enter fullscreen mode Exit fullscreen mode

Resource: Learn Google Flutter Fast - Mark Clow ✨

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more