angelina
angelina

Reputation: 109

No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?

I am getting this above error whenever i am trying to route to another page in flutter.

Following is my code

onPressed: () {
  Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => MyHomePage(),
      ));
  },

Upvotes: 5

Views: 3242

Answers (2)

Maxim
Maxim

Reputation: 486

I got this error when I tried to use

S.of(context)...

inside

return MaterialApp(...)

In all other places

S.of(context)...

does not return error

Upvotes: 1

Lausbert
Lausbert

Reputation: 1590

After several hours of tracking this down on my own, this lead me to a solution. Mixing absolute and relative imports lead to more than one instances of Type of S. I am not talking about instances of S.

In short:

Make sure to import via an absolute path

 import 'package:[YOUR_PACKAGE_NAME]/generated/l10n.dart'

in every file where you are using S. This should avoid both of these messages:

'No instance of S was loaded. Try to initialize the S delegate before accessing S.current.
'No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?'

I guess only relative paths might also work, but since auto imports are absolute this might lead to an error later on. Except for the absolute path you should simply follow the official setup.

Upvotes: 1

Related Questions