You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter/Dart国际化无法完全访问:S.current调用报错求助

Flutter 国际化报错:S.current 未初始化问题

我用 Flutter + Dart 开发网页并添加国际化支持,S.current.name 能正常传递为网站标题,但构建组件时出现如下错误:

Assertion failed: file:///C:/dev/Adri/lib/generated/l10n.dart:21:12
_current != null
"No instance of S was loaded. Try to initialize the S delegate before accessing S.current."

我的 main() 和 HomePage 代码:

void main() {
  runApp(const HomeScreen());
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<LanguageChangeProvider>(
      create: (context) => LanguageChangeProvider(),
      child: Builder(
        builder: (context) => MaterialApp(
            debugShowCheckedModeBanner: false,
            locale: Provider.of<LanguageChangeProvider>(context, listen: true)
                .currentLocale,
            localizationsDelegates: const [
              S.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate
            ],
            supportedLocales: S.delegate.supportedLocales,
            //title: S.current.name,
            home: MyHomePage(title: S.current.name,)),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  
  final String title;

  @override
  State<StatefulWidget> createState() => _MyHomePageState();
}

可以看到S.current.name能成功传递为网站标题,但HomeScreen其余部分无法访问S,以下是_MyHomePageState代码:

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          actions: [
            IconButton(
                tooltip: SELECT_LANG_HU,
                onPressed: () {
                  languageChangeProcess(context, LOCALE_HU);
                },
                icon: const Icon(Icons.language)),
            IconButton(
                tooltip: SELECT_LANG_EN,
                onPressed: () {
                  languageChangeProcess(context, LOCALE_EN);
                },
                icon: const Icon(Icons.change_circle))
          ],
          iconTheme: const IconThemeData(color: Colors.grey),
          title: Text(
            S.current.name,
            style: TextStyle(color: Colors.grey.shade600),
          ),
          flexibleSpace: Container(
            decoration: BoxDecoration(
                gradient: LinearGradient(colors: <Color>[
              Colors.teal.shade50,
              Colors.teal.shade100,
            ])),
          ),
        ),
        body: Row(
          children: [Expanded(flex: 1, child: Container())],
        ),
        drawer: Drawer(
          child: ListView(
            children: [
              DrawerHeader(
                  decoration: BoxDecoration(
                      gradient: LinearGradient(colors: <Color>[
                    Colors.teal.shade50,
                    Colors.teal.shade100
                  ])),
                  padding:
                      const EdgeInsets.only(left: 2.0, right: 2.0, top: 4.0),
                  child: const ContactCard()),
              MenuElement(Icons.person, S.current.introduction, () => {}),
              MenuElement(Icons.school, S.current.education, () => {}),
              MenuElement(
                  Icons.workspace_premium, S.current.publications, () => {}),
              MenuElement(Icons.work, S.current.workExperience, () => {}),
            ],
          ),
        ));
  }
}

pubspec.yaml 中已添加依赖:

flutter_localizations:
    sdk: flutter
  provider: ^5.0.0

请问我是否遗漏了什么明显的配置?


内容的提问来源于stack exchange,提问作者Zoltán Györkei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 07:40:40