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:
- 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.
- In the version 2 code,S.of(context) is returning null.
Blockquote
Solution 1: Ares Junior
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.