5

I'm using the i18n plugin for Flutter (I believe it's this one) that comes with Android Studio.

And in every example I see it says to use S.of(context).my_string to get the Strings but it always returns null.

If I use S.current.my_string, it seems to work.

So is S.current the right way to do it and every doc/tutorial out there is wrong, are they the same or what?

What I'm basically asking here, is what is the difference between them.

Dpedrinha
  • 3,741
  • 3
  • 38
  • 57
  • 1
    `S.of(context)` works fine for me so I guess it must be another reason. Can you show some code example? – GrahamD Oct 02 '19 at 20:34
  • Nothing really relevant to show. It happens even if I create a new project, add the delegates to MaterialApp, extract an existing String and try using it with S.of(context).my_string fails. – Dpedrinha Oct 03 '19 at 14:31

2 Answers2

3

Seems like S.of(context) is initially available way to access localised string.

But sometimes you need to use it without Build Context (in ViewModel, for example). So S.current was added for these cases.

More info here

o0sea0o
  • 191
  • 2
  • 5
0

S.of(context) and S.current are both used to access the translated strings defined in a localization delegate.

S.current is a static reference to the current localized resources. when we don't have context easily available or can't access BuildContext that case S.current can be used.

S.of(context) is a method used to access localized resources based on provided context.

Akshay
  • 1
  • 1