我正在使用這個包插件flutter_locales進行應用程式本地化。因此,使用此代碼檢查當前語言代碼。
LocaleNotifier.of(context)!.locale!.languageCode;
現在的問題是,如果我在簡單的類中使用此代碼,則會在“背景關系”上出現錯誤。
class Service {
var lang = LocaleNotifier.of(context)!.locale!.languageCode; // getting error on "context"
}
那么如何在 Service 類中沒有 BuildContext 的情況下防止這種“背景關系”錯誤呢?
uj5u.com熱心網友回復:
你根本就沒有。您需要將 傳遞BuildContext context給您的Service班級。
嘗試這個:
class Service {
final BuildContext context;
Service(this.context);
String get lang => LocaleNotifier.of(context)!.locale!.languageCode;
}
然后在創建這個類時傳入背景關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339523.html
上一篇:FlutterDart-'Map<dynamic,dynamic>'不能分配給引數型別'Map<int,Color>'
