Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Flutter Constants Best Practices
Gülsen Keskin
Gülsen Keskin

Posted on • Edited on

     

Flutter Constants Best Practices

Flutter da tüm constant'ları bir dosyada tutmanın en iyi yolu nedir?

Java ve C#'da her tanım bir sınıfın içinde olmalıdır, bu nedenle yalnızca statik üyelerin var olduğu sınıflar yaygın olarak kullanılır.

Dart'ın üst düzey işlevleri, değişkenleri ve sabitleri vardır, bu nedenle yalnızca bir şeyi tanımlamak için bir sınıfa ihtiyacınız yoktur. İstediğiniz bir namespace ise kitaplık(library) daha uygundur. Kitaplıklar içe aktarma öneklerini ve birleştiricileri show/hide destekler. Bunlar, kodunuzun tüketicisinin ad çakışmalarını kendileri için en iyi şekilde işlemesini sağlayan güçlü araçlardır.

Bir fonksiyon veya değişken mantıksal olarak bir sınıfa bağlı değilse, onu en üst düzeye koyun. Ad çakışmalarından endişe ediyorsanız, ona daha kesin bir ad verin veya önekle import edilebilen ayrı bir library'e taşıyın.

Doğru kullanım:

DateTime mostRecent(List<DateTime> dates) {  return dates.reduce((a, b) => a.isAfter(b) ? a : b);}const _favoriteMammal = 'weasel';
Enter fullscreen modeExit fullscreen mode

Yanlış kullanım:

class DateUtils {  static DateTime mostRecent(List<DateTime> dates) {    return dates.reduce((a, b) => a.isAfter(b) ? a : b);  }}class _Favorites {  static const mammal = 'weasel';}
Enter fullscreen modeExit fullscreen mode

Örnek Uygulama:

constants.dart adında yeni bir dosya oluşturun.

Aşağıda görüldüğü gibi constantlarınızı bildirin.

 const succesMessage=" İşlem başarılı."; // Api related  const apiBaseURL = "https://baseurl.com"; const userLoginApi = "login"; const userSignupApi = "signup"; // Shared Preference keys  const kDeviceName = "device_name"; const kDeviceUDID = "device_id"; // Asset Constants const navBarLogoImage = "assets/images/home_images/sample.png"
Enter fullscreen modeExit fullscreen mode

Stil kurallarında belirtildiği gibi constantlarınızı isimlendirirkenlowerCamelCase biçimlendirmesini kullandığınızdan emin olun. bkz: navBarLogoImage

Ardından, sabitlere erişmesi gereken herhangi bir dart dosyasının en üstüne aşağıdaki import ifadesini ekleyin:

import 'constants.dart' as constants;

constants.dart dosyanız farklı bir dizindeyse , import ifadenizdeconstants.dart yolunu belirtmeniz gerektiğini unutmayın.

Absolute path örneği:

import 'package:<your_app_name>/common/constants.dart' as constants;

Artık bu sözdizimi ile sabitlerinize kolayca erişebilirsiniz:

String a = constants.succesMessage;

References:
https://dart.dev/guides/language/effective-dart/design
https://dart.dev/guides/language/effective-dart/style
https://stackoverflow.com/questions/54069239/whats-the-best-practice-to-keep-all-the-constants-in-flutter
https://flutteragency.com/keep-all-the-constants-in-flutter/#:~:text=The%20preferred%20solution%20is%20to,new%20dart%20file%20named%20Constants.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

soft. dev. at harmonycloud
  • Location
    Sakarya, Türkiye
  • Education
    Pamukkale University
  • Work
    harmonyerp
  • Joined

More fromGülsen Keskin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp