0

Version 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),
      ),
    );
}

When I run this code, I got error :

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.

So after searching on stack overflow, I got this link,

then code becomes:

Version 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),
          ),
        );

      });
    });

  }

Then I got error:

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): 

Questions:

  1. In the version 1 code, I believe I am getting error because buildContext is not available till then, but as mentioned here, in initstate method if mounted then buildContext is there.
  2. In the version 2 code,S.of(context) is returning null.

Blockquote

Ankur_009
  • 3,823
  • 4
  • 31
  • 47
  • 1
    Don't build widgets (assuming `Slide` is a widget) in `initState`. They should be built in `build`. – Richard Heap Aug 22 '19 at 03:11
  • @RichardHeap then why negative margin error is coming – Ankur_009 Aug 22 '19 at 04:17
  • Ensure that you inited localizations correctly. `localizationsDelegates: [ S.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: S.delegate.supportedLocales,` – Roman Soviak Aug 26 '20 at 10:16

1 Answers1

1

Had the same problem, what I did was create a class to extend "S", override the method "of", which is the one that is returning null and use the "current" attribute of "S".

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

And now I use

R.of(context).app_name

Guess what? It worked! I tested it by changing the language and opening the app again and it works like a charm, changing the language properly. Somehow the property "current" keeps the right implementation of "of", I just used it to help me. Hope it helps.

  • 1
    yay, that saved my day! thanks! i made it shorter, i added a var: var i10n = (S.of(context) == null)?S.current: S.of(context); makes the code shorter, since the i only have i10n,title instead of S.of(context).title.... – bboett Jun 26 '21 at 15:22