简体   繁体   English

Dart 枚举扩展未更新

[英]Dart enum extension isn't updating

I have the following code where I want to make sure my status will be localized in app, so I wanted to parse current locale and get value in needed language, but what it basically do, is simply remembering first language (locale) value parsed in it and staying with it till I restart my app我有以下代码,我想确保我的状态将在应用程序中本地化,所以我想解析当前语言环境并获取所需语言的值,但它基本上做的只是记住解析的第一语言(语言环境)值它并坚持下去,直到我重新启动我的应用程序

enum BillStatus {
  sent,
  processed,
  ...
}

extension BillStatusExtension on BillStatus {

  static Map<BillStatus, String> names = {
    BillStatus.sent: S.current.sent,
    BillStatus.processed: S.current.processed,
    ...
  };

  String? get name => names[this];
}

S.current means AppLocalizationDelegate provided with intl package S.current表示AppLocalizationDelegate提供intl package

When you do:当你这样做时:

 extension BillStatusExtension on BillStatus { static Map<BillStatus, String> names = { BillStatus.sent: S.current.sent, BillStatus.processed: S.current.processed, ... }; String? get name => names[this]; }

You're creating a names Map that's lazily initialized with whatever the values are of S.current.sent , S.current.processed , etc. are at the time names is first accessed.您正在创建一个names Map ,它在首次访问names时使用S.current.sentS.current.processed等的任何值进行延迟初始化。 When the locale changes, that Map will never be updated and still will be referencing the same String objects.当语言环境发生变化时, Map将永远不会更新,并且仍然会引用相同的String对象。

You should do one of:您应该执行以下操作之一:

  • Look up S.current.sent , S.current.processed , etc. dynamically.动态查找S.current.sentS.current.processed等。 An easy way to do this would be to change your Map to store Function s instead of String s:一种简单的方法是将Map更改为存储Function而不是String

     extension BillStatusExtension on BillStatus { static Map<BillStatus, String Function()> names = { BillStatus.sent: () => S.current.sent, BillStatus.processed: () => S.current.processed, ... }; String? get name => names[this]?.call(); }
  • Alternatively explicitly reinitialize the Map when the locale changes.或者,在语言环境更改时显式重新初始化Map This is more work and likely is overkill in this case, but it's a technique that you can use if recomputing values on every lookup is too expensive:这是更多的工作,在这种情况下可能有点矫枉过正,但如果在每次查找时重新计算值过于昂贵,则可以使用这种技术:

     extension BillStatusExtension on BillStatus { static Map<BillStatus, String> names = {}; static String _lastLocale = ''; static void _initializeNames() { names[BillStatus.sent] = S.current.sent; names[BillStatus.processed] = S.current.processed; ... } String? get name { if (locale;= _lastLocale) { _initializeNames(); _lastLocale = locale; } return names[this]; } }

    Instead of reinitializing your Map lazily, you also could register a callback to do it immediately when the locale changes.除了懒惰地重新初始化Map ,您还可以注册一个回调,以便在语言环境更改时立即执行此操作。 See Is it possible to listen for system language change in Flutter?请参阅是否可以在 Flutter 中监听系统语言更改?

Your code should look like this:您的代码应如下所示:

void main() {
    final status = BillStatus.sent;
    print(status.name); // Sent
}

enum BillStatus { sent }

class BillStatusHelper {
  static const Map<BillStatus, String> names = {
    BillStatus.sent: 'Sent',
  };
}

extension BillStatusExtension on BillStatus {
  String get name => BillStatusHelper.names[this] ?? 'Unknown';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM