- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我尝试使用本地化包在 Flutter 中本地化一个字符串。问题是需要我翻译的位置。它与 UI 无关,而是在我模型的某个深处,我无权访问 BuildContext。有没有其他的可能继续使用翻译功能?
// I don't have a context variable here
MyLocalizations.of(context).trans("foo")
最佳答案
是的。您不需要 BuildContext 来访问字符串。这是我的解决方案:
class Strings {
Strings._(Locale locale) : _localeName = locale.toString() {
current = this;
}
final String _localeName;
static Strings current;
static Future<Strings> load(Locale locale) async {
await initializeMessages(locale.toString());
final result = Strings._(locale);
return result;
}
static Strings of(BuildContext context) {
return Localizations.of<Strings>(context, Strings);
}
String get title {
return Intl.message(
'Hello World',
name: 'title',
desc: 'Title for the Demo application',
);
}
}
Future<Null> main() async {
final Locale myLocale = Locale(window.locale);
await Strings.load(myLocale);
runApp(MyApplication());
}
现在你可以像下面这样引用一个字符串:
final title = Strings.current.title;
关于dart - 在 Flutter 中获取 BuildContext 以进行本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51803755/
我有一个凸起的按钮,它带有一个在我的AuthenticationProvider中执行函数的闭包 RaisedButton( textColor: Theme.of(context).backgr
我想知道为什么会收到此异常以及如何解决该异常?代码段会有所帮助 我正在使用Flutter(Linux上的Channel beta v0.6.0,语言环境为en_US.UTF-8) Flutter医生显
我正在尝试实现自动完成字段,但是泛型有一点问题。这是我的实现: import 'package:flutter/material.dart'; import 'package:kiwi_mobile/
这是一个代码: import 'package:flutter/material.dart'; import 'package:flutterapp/ui/pages/notes_home.dart'
在一个 flutter 应用程序中,假设我有一个内部有多个小部件的小部件类(即,只是一个典型的单个长 Widget build() 类,内部有多个小部件)。然后这些被分成多个子部件,有自己的类:例如,
目前我从 HomeScreen 的 build 方法中获取 BuildContext,然后我必须将它传递给 _gridSliver 然后向下到 _storeCard。 我如何编写代码才能不需要向下传递
我在 Flutter 中创建了一个扩展 AppBar 类的类,这样我就可以在需要时重用它。我的问题是如何访问有状态/无状态小部件构建上下文? class AppBarLayout extends Ap
我注意到状态的构建方法传入了一个 BuildContext,我还注意到状态本身也有一个名为 context 的成员。我在想什么时候用BuildContext合适,什么时候用成员变量合适?它们可以互换吗
我已经创建了一个注册函数,但在 Utils.flushBarErrorMessage("No Internet", context);Do not use BuildContexts across a
有没有办法在不访问 BuildContext 的情况下在函数中加载 Flutter 中的图像? Flutter 可以使用precacheImage() 预加载图片,需要BuildContext,使用不
以下代码是一个简单的示例,仅用于说明目的。 Scaffold.of(...)不能使用最外面的BuildContext因为一个众所周知的原因。这就是为什么我必须包装 Scaffold.of(...)与
是否有不需要上下文的 Navigator.push 替代方案?这是我的问题。 我有一个嵌套很深的调用堆栈,并不是每个函数都有一个 BuildContext 参数。除了将当前 BuildContext
我正在 Flutter 中开发一个应用程序(我还是个新手),但遇到以下错误: Error: Could not find the correct Provider> above this Exerci
此编译错误:错误:无法将参数类型“上下文”分配给参数类型“BuildContext”(在[tter] lib \ Pages \ list_view.dart:95处为argument_type_no
我正在制作一个具有不同构建方法的应用程序,以便我可以制作一个项目列表,以便在点击“爱心”按钮时将它们保存在另一个屏幕上。但是我在代码中遇到错误。我正在关注 Flutter Codelabs app t
我在上下文中遇到错误,我在stackoverflow上也发现了类似的问题,但并没有解决我的问题。该问题的答案建议添加import 'package:path/path.dart';,但仍然出现相同的错
我使用 flushbar向用户显示应用内通知,但我认为这个问题适用于任何想要覆盖在当前屏幕上的小部件,例如 snackbar 。 我希望能够显示独立于屏幕之间导航的通知,即,如果用户在屏幕之间导航,则
我在 Dart 类中有一个方法,它接受 BuildContext 参数,如下所示: class MyClass { myMethodName(BuildContext context, ...)
BuildContext 做了什么,我们从中得到了什么信息? https://docs.flutter.dev/flutter/widgets/BuildContext-class.html只是不清楚
基本上,当用户在对话框中按下注销时,我想返回到我的登录 View 。 onSelected: (value) async { switch (value) {
我是一名优秀的程序员,十分优秀!