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

Flutter添加flutter_localization依赖报错:SDK中找不到该包

Flutter本地化依赖报错解决方案

问题描述

添加本地化依赖时出现如下错误:

由于timer依赖SDK中的flutter_localization(Flutter SDK中找不到该包),版本解析失败。

已确认Android Studio中Flutter和Dart SDK路径配置正确,当前pubspec.yaml内容如下:

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.0.0+1

environment:
  sdk: '>=2.18.2 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.17.0-nullsafety.2
  cupertino_icons: ^1.0.2
  sqflite: any
  shared_preferences: ^2.0.15
  path_provider: any
  flutter_localization:
    sdk: flutter
  flutter_cupertino_localizations: ^1.0.1

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0

flutter:

  uses-material-design: true

需要依赖包来访问GlobalMaterialLocalizations,已尝试版本兼容性检查、依赖更新、SDK升级、重装Flutter和Dart等操作,问题仍未解决。

解决方案

  1. 修正依赖包名称拼写
    Flutter官方提供的本地化依赖包是复数形式的flutter_localizations,而非你配置的单数flutter_localization,这是核心问题。修改pubspec.yaml中的对应依赖项:

    dependencies:
      # 保留其他原有依赖
      flutter_localizations:
        sdk: flutter
    
  2. 清理缓存并重新获取依赖
    执行以下命令清除项目缓存,重新拉取正确的依赖:

    flutter clean
    flutter pub get
    
  3. 确认SDK版本匹配
    检查当前Flutter SDK版本是否符合environment中的约束(>=2.18.2 <3.0.0),执行flutter --version查看版本,若版本过低,运行flutter upgrade升级到对应稳定版。

  4. 验证本地化基础配置
    在main.dart中确保正确配置本地化代理,示例代码如下:

    import 'package:flutter_localizations/flutter_localizations.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('en', 'US'),
            Locale('zh', 'CN'),
          ],
          home: const HomePage(),
        );
      }
    }
    

内容的提问来源于stack exchange,提问作者IKnowNothingAboutFlutter

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 17:15:46