【问题标题】:S.of(context) return nullS.of(context) 返回 null
【发布时间】:2019-12-27 00:59:33
【问题描述】:

版本 1:

 @override
  void initState() {
    super.initState();

    slides.add(
      new Slide(
        title: S.of(context).intro_title_first,
        description: S.of(context).intro_description_first,
        pathImage:"images/image1",

        /*pathImage: "assets/images/intro_1.xml",*/

        backgroundColor: Color(0xfff5a623),
      ),
    );
}

当我运行这段代码时,我得到了错误:

I/flutter ( 9492): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 9492): The following assertion was thrown building Builder:
I/flutter ( 9492): inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was called before
I/flutter ( 9492): IntroScreenState.initState() completed.
I/flutter ( 9492): When an inherited widget changes, for example if the value of Theme.of() changes, its dependent
I/flutter ( 9492): widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor
I/flutter ( 9492): or an initState() method, then the rebuilt dependent widget will not reflect the changes in the
I/flutter ( 9492): inherited widget.
I/flutter ( 9492): Typically references to inherited widgets should occur in widget build() methods. Alternatively,
I/flutter ( 9492): initialization based on inherited widgets can be placed in the didChangeDependencies method, which
I/flutter ( 9492): is called after initState and whenever the dependencies change thereafter.

所以在搜索堆栈溢出后,我得到了this link

那么代码就变成了:

版本 2:

@override
  void initState() {
    super.initState();

    Future.delayed(const Duration(milliseconds: 500), () {
      setState(() {
        // Here you can write your code for open new view

        slides.add(
          new Slide(
            title: S.of(context).intro_title_first,
            description: S.of(context).intro_description_first,
            pathImage:"images/image1",

            /*pathImage: "assets/images/intro_1.xml",*/

            backgroundColor: Color(0xfff5a623),
          ),
        );

      });
    });

  }

然后我得到错误:

The following assertion was thrown building IntroSlider(dirty, dependencies: [MediaQuery,
I/flutter ( 9492): _LocalizationsScope-[GlobalKey#bb3bb]], state: IntroSliderState#f24e7(ticker inactive)):
I/flutter ( 9492): 'package:flutter/src/widgets/container.dart': Failed assertion: line 267 pos 15: 'margin == null ||
I/flutter ( 9492): margin.isNonNegative': is not true.
I/flutter ( 9492): 

问题:

  1. 在版本 1 代码中,我相信我收到了错误,因为 buildContext 在此之前不可用,但作为 mentioned here,在 initstate 方法中,如果已安装则 buildContext 存在。
  2. 在版本 2 代码中,S.of(context) 返回 null。

块引用

【问题讨论】:

  • 不要在initState 中构建小部件(假设Slide 是小部件)。它们应该建在build
  • @RichardHeap 那么为什么会出现负边距错误
  • 确保您正确启动了本地化。 localizationsDelegates: [ S.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: S.delegate.supportedLocales,

标签: flutter flutter-layout


【解决方案1】:

有同样的问题,我所做的是创建一个类来扩展“S”,覆盖方法“of”,这是返回null的方法并使用“S”的“current”属性。

class R extends S {
  static of(BuildContext context) {
    return S.current;
  }
}

现在我用

R.of(context).app_name

你猜怎么着?有效! 我通过更改语言并再次打开应用程序对其进行了测试,它就像一个魅力,正确地改变了语言。不知何故,属性“current”保持了“of”的正确实现,我只是用它来帮助我。 希望对您有所帮助。

【讨论】:

  • 耶,这拯救了我的一天!谢谢!我让它更短,我添加了一个 var: var i10n = (S.of(context) == null)?S.current: S.of(context);使代码更短,因为 i 只有 i10n,title 而不是 S.of(context).title....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
相关资源
最近更新 更多