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 AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay