flutter 最好的空安全AppLocalization字符串方法

0md85ypi  于 2023-05-19  发布在  Flutter
关注(0)|答案(5)|浏览(223)

提问

我正在使用AppLocalizations.of(context).myString在我的null safe flutter应用程序中国际化字符串。
我的IDE告诉我AppLocalizations.of(context)可以返回null。处理这件事的最好办法是什么?有没有办法确保AppLocalizations.of(context)永远不会返回null?
目前,我采用以下方法:

AppLocalizations.of(context)?.myString ?? 'Fallback string'

完整项目代码

Pubspec.yaml

name: Sample Intl Project
description: A sample project
publish_to: 'none'
version: 1.0.0+1

environment:
  sdk: ">=2.12.0-133.2.beta <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.17.0-nullsafety.2

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
  generate: true

l10n.yaml

arb-dir: lib/l10n
template-arb-file: app_en_US.arb
output-localization-file: app_localizations.dart

l10n/app_en_US.arb

{
  "helloWorld": "Hello World!",
  "@helloWorld": {
    "description": "Greeting"
}

l10n/app_en.arb

{
  "helloWorld": "Hello World!"
}

main.dart

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sample App',
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: AppLocalizations.supportedLocales,
      home: Home()
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Text(
        AppLocalizations.of(context)?.helloWorld ?? 'Hello World!'
      ),
    );
  }
}
yxyvkwin

yxyvkwin1#

在l10n.yaml中添加这一行:

nullable-getter: false
k75qkfdt

k75qkfdt2#

如果你确定AppLocalizations.of(context)总是会返回一个有效的AppLocalizations示例(对于你的示例应用程序来说也是如此),你可以用途:

AppLocalizations.of(context)!.myString

!运算符告诉Dart,AppLocalizations.of(context)将永远不会返回null,其作用类似于从可空的AppLocalizations?转换为不可空的AppLocalizations
注意:如果AppLocalizations.of(context)在运行时返回null,则AppLocalizations.of(context)!将抛出异常。

anhgbhbe

anhgbhbe3#

正如在评论中所说,我希望你能分享更多的代码,你的pubpsec,你的l10n.yaml和任何其他相关的东西。
在此之前,我的建议是将依赖项中的intl从latest更新为:
intl: ^0.17.0-nullsafety.2
我认为nullsafety不会留给最终的开发者,它很快就会成为标准,而不必考虑它。

yi0zb3m4

yi0zb3m44#

看起来,你的应用总是期望AppLocalizations文本中的string值。
我建议确保string是空安全的,这应该可以解决你的问题。
以下是您可以使用的片段。
1.定义字符串扩展名

// a string extension
extension MyStringExtentions on String {
  String nullSafeStr() =>
      (this == null || this.isEmpty || this == "null") ? "" : this;
}

// OR a static method
//static String nullSafeStr(String source) => (source == null || source.isEmpty || source == "null") ? "" : source;

现在您可以使用它们中的任何一个,请参见使用新扩展名的示例

// .nullSafeStr() becomes available wherever you import the extension method 
AppLocalizations.of(context).helloWorld.nullSafeStr()

// the alternative way is to call the static method  
.nullSafeStr(AppLocalizations.of(context).helloWorld);
ygya80vv

ygya80vv5#

(我不允许发表评论,所以我回答)
添加键/值

nullable-getter: false

l10n.yaml(位于项目的根目录)。
这将自动对AppLocalizations.of(context)进行空检查,允许程序员只编写AppLocalizations.of(context).<key>而不是AppLocalizations.of(context)!.<key>
来自Flutter的文档:
[no-]nullable-getter
指定本地化类getter是否可为空。
默认情况下,此值为true,以便Localizations.of(context)返回可为空的值,以实现向后兼容。如果该值为false,则对Localizations.of(context)的返回值执行空值检查,从而消除了在用户代码中进行空值检查的需要。

相关问题