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

Firebase升级后运行Flutter模拟器出现多项编译错误求助

Firebase升级后Flutter应用编译失败的解决方案

错误日志(中文翻译)

启动lib\main.dart在sdk gphone64 x86 64的debug模式下...
正在运行Gradle任务'assembleDebug'...
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:19:7: 错误:函数声明需要显式参数列表。
请尝试为函数声明添加参数列表。
await Firebase.initializeApp(
^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:19:15: 错误:此处之前应出现'{'。
await Firebase.initializeApp(
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:19:15: 错误:预期是声明,但得到了'.'。
await Firebase.initializeApp(
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:20:8: 错误:非可选参数不能有默认值。
请尝试移除默认值或将参数设为可选。
options: DefaultFirebaseOptions.currentPlatform,
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:21:2: 错误:预期是函数体或'=>'。
请尝试添加{}。
);
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:23:1: 错误:指令必须出现在所有声明之前。
请尝试将指令移至所有声明之前。
part 'src/firebase_app.dart';
^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:24:1: 错误:指令必须出现在所有声明之前。
请尝试将指令移至所有声明之前。
part 'src/firebase.dart';
^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:15:8: 错误:读取'/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_options.dart'时出错:系统找不到指定文件。

import 'firebase_options.dart';
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/src/firebase.dart:9:7: 错误:此作用域中已声明'Firebase'。
class Firebase {
^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:19:7: 上下文:'Firebase'的先前声明。
await Firebase.initializeApp(
^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:19:1: 错误:未找到类型'await'。
await Firebase.initializeApp(
^^^^^
lib/main.dart:12:9: 错误:无法使用'Firebase',因为它被多次声明。
await Firebase.initializeApp(
^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/firebase_core.dart:20:10: 错误:未定义名称'DefaultFirebaseOptions'。
options: DefaultFirebaseOptions.currentPlatform,
^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/src/firebase.dart:66:19: 错误:无法使用'Firebase',因为它被多次声明。
if (other is! Firebase) return false;
^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_core-1.24.0/lib/src/firebase.dart:75:26: 错误:无法使用'Firebase',因为它被多次声明。
String toString() => '$Firebase';
^

FAILURE: 构建因异常而失败。

- 位置:
脚本'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle'第1159行

- 出错原因:
任务':app:compileFlutterBuildDebug'执行失败。

> 进程'命令'C:\src\flutter\bin\flutter.bat''以非零退出值1结束

- 尝试方案:

> 使用--stacktrace选项获取堆栈跟踪。
> 使用--info或--debug选项获取更多日志输出。
> 使用--scan选项获取完整洞察。

BUILD FAILED in 49s
Exception: Gradle任务assembleDebug失败,退出代码1

问题原因

从日志可见,firebase_core包的核心文件已损坏(官方包的firebase_core.dart中出现了本该在用户代码里的初始化逻辑,同时存在文件缺失、类重复声明等问题),大概率是Firebase升级过程中Pub缓存的依赖包损坏,或是依赖版本不兼容导致。

解决步骤

  • 清理Flutter Pub缓存:
    flutter pub cache clean
    
  • 删除项目根目录下的pubspec.lock文件
  • 重新获取项目依赖:
    flutter pub get
    
  • 检查并修正main.dart中的Firebase初始化代码,确保结构正确(需先执行WidgetsFlutterBinding.ensureInitialized()):
    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'firebase_options.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(primarySwatch: Colors.blue),
          home: const Scaffold(body: Center(child: Text('Hello'))),
        );
      }
    }
    
  • 确认lib/firebase_options.dart文件存在,若缺失则重新运行Firebase配置命令生成:
    flutterfire configure
    
  • 清理Android端Gradle缓存(进入项目的android目录后执行):
    ./gradlew clean
    
  • 最后重新运行应用:
    flutter run
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 06:55:18