Flutter S.of(上下文)返回null

Flutter S.of(上下文)返回null,flutter,flutter-layout,Flutter,Flutter Layout,第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", /*pa

第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.
在搜索堆栈溢出后,我得到

然后代码变成:

第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代码中,我相信我得到了错误,因为buildContext在此之前不可用,但在initstate方法中,如果装载了buildContext,则buildContext就在那里。 在版本2代码中,S.ofcontext返回null。
Blockquote也有同样的问题,我所做的是创建一个类来扩展S,重写的方法,该方法返回null并使用S的当前属性

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

R.ofcontext.app\u名称

你猜怎么着?成功了! 我通过改变语言并再次打开应用程序来测试它,它就像一个魔咒,正确地改变语言。不知何故,属性current保持了正确的实现,我只是用它来帮助我。
希望有帮助。

不要假设幻灯片是处于初始状态的小部件,就构建小部件。它们应该是内置的。@RichardHeap那么为什么会出现负边距错误呢?请确保您正确初始化了本地化。本地化委派:[S.delegate,GlobalMaterialAllocalizations.delegate,GlobalWidgetsLocalizations.delegate,GlobalSuperTinoLocations.delegate],supportedLocales:S.delegate.supportedLocales,