DEV Community

Cover image for Flutter Mixin Class
Gülsen Keskin
Gülsen Keskin

Posted on • Edited on

2

Flutter Mixin Class

Flutter'da Mixin sınıfını kullanmak, birden çok sınıf arasında kod paylaşmak için faydalı olabilir. Bu, kodun tekrarlanmasını önlemeye yardımcı olabilir ve kodunuzu daha düzenli ve bakım yapılabilir hale getirebilir. Ek olarak, Mixin'i kullanmak, aynı kodu birden çok kez yazmak yerine ortak kodu yeniden kullanmanıza izin vererek uygulamanızın performansını artırmanıza yardımcı olabilir. Bu ayrıca, birden çok sınıfı güncellemek yerine değişiklikleri tek bir yerde yapmanız gerektiğinden, kodunuzda değişiklik yapmayı da kolaylaştırabilir. Genel olarak, Mixin'i kullanmak, kodunuzu daha verimli ve yönetimi kolay hale getirmenize yardımcı olabilir.

Örnek:

class MyMixin {
  String getText() {
    return "Hello World!";
  }
}

class MyClass1 with MyMixin {
  void printText() {
    print(getText());
  }
}

class MyClass2 with MyMixin {
  void printText() {
    print(getText());
  }
}

void main() {
  MyClass1 myClass1 = MyClass1();
  myClass1.printText();

  MyClass2 myClass2 = MyClass2();
  myClass2.printText();
}

Enter fullscreen mode Exit fullscreen mode

Bu örnekte MyMixin sınıfı, "Hello World!" stringini döndüren getText() methodunu içerir. MyClass1 ve MyClass2 sınıflarının her ikisi de, getText() methodunu her iki sınıfta da kullanılabilir kılan MyMixin sınıfını dahil etmek için with anahtar sözcüğünü kullanır. Her sınıftaki printText() yöntemi, string ifadeyi konsola yazdırmak için MyMixin sınıfından getText() methodunu kullanır.

Kaynak :)

Sentry mobile image

Mobile Vitals: A first step to Faster Apps

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read the guide →

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