[Solved] Flutter Intl Plugin Error: No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?

2023/02/12 22:57

[Solved] Flutter Intl Plugin Error: No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?

Widget build(BuildContext context) {
  return MaterialApp(
    title: 'Flutter Demo',
    localizationsDelegates: const [
      GlobalMaterialLocalizations.delegate,
      GlobalWidgetsLocalizations.delegate,
      GlobalCupertinoLocalizations.delegate,
      S.delegate,

    ],
    supportedLocales: S.delegate.supportedLocales,
    localeListResolutionCallback: (locales, supportedLocales) {
      print('当前系统语言环境$locales');
      return;
    },
    onGenerateTitle: (context){
      return S.of(context).title;
    },

    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    //home: MyHomePage(title: S.of(context).title),
    home: MyHomePage(title: S.current.title)
  );
}

 

Error

The following assertion was thrown building MyApp(dirty):
No instance of S present in the widget tree. Did you add S.delegate in localizationsDelegates?
‘package:untitled/generated/l10n.dart’:
Failed assertion: line 44 pos 12: ‘instance != null’

Solution

Change this:

//home: MyHomePage(title: S.of(context).title),

to:

home: MyHomePage(title: S.current.title)

Leave a Reply

Back to top