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

Flutter Web应用浏览器显示空白页问题求助

问题排查:Flutter Web部署后显示Firebase默认页面而非应用内容

问题描述

Flutter应用在Android端运行正常,但构建Web版本部署到服务器后,打开时显示Firebase Hosting的默认欢迎页面,带有"Open Deal Diligence"按钮,点击后仅打开相同的欢迎页面。尝试修改<base href="/">无效,怀疑index.html脚本未执行,且注意到Firebase SDK初始化代码被注释。

提供的文件内容

index.html(当前使用的Firebase默认页面)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Welcome to Firebase Hosting</title>

    <!-- update the version number as needed -->
    <script defer src="/__/firebase/10.12.2/firebase-app-compat.js"></script>
    <!-- include only the Firebase features as you need -->
    <script defer src="/__/firebase/10.12.2/firebase-auth-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-database-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-firestore-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-functions-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-messaging-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-storage-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-analytics-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-remote-config-compat.js"></script>
    <script defer src="/__/firebase/10.12.2/firebase-performance-compat.js"></script>
    <!-- 
      initialize the SDK after all desired features are loaded, set useEmulator to false
      to avoid connecting the SDK to running emulators.
    -->
    <script defer src="/__/firebase/init.js?useEmulator=true"></script>

    <style media="screen">
      body { background: #ECEFF1; color: rgba(0,0,0,0.87); font-family: Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 0; }
      #message { background: white; max-width: 360px; margin: 100px auto 16px; padding: 32px 24px; border-radius: 3px; }
      #message h2 { color: #ffa100; font-weight: bold; font-size: 16px; margin: 0 0 8px; }
      #message h1 { font-size: 22px; font-weight: 300; color: rgba(0,0,0,0.6); margin: 0 0 16px;}
      #message p { line-height: 140%; margin: 16px 0 24px; font-size: 14px; }
      #message a { display: block; text-align: center; background: #039be5; text-transform: uppercase; text-decoration: none; color: white; padding: 16px; border-radius: 4px; }
      #message, #message a { box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); }
      #load { color: rgba(0,0,0,0.4); text-align: center; font-size: 13px; }
      @media (max-width: 600px) {
        body, #message { margin-top: 0; background: white; box-shadow: none; }
        body { border-top: 16px solid #ffa100; }
      }
    </style>
  </head>
  <body>
    <div id="message">
      <h2>Welcome</h2>
      <h1>Firebase Hosting Setup Complete</h1>
      <p>You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!</p>
      <a target="_blank" href="https://deal-diligence-5de73.web.app/index.html">Open Deal Diligence</a>
    </div>
    <p id="load">Firebase SDK Loading&hellip;</p>

    <script>
      document.addEventListener('DOMContentLoaded', function() {
        const loadEl = document.querySelector('#load');
        // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥
        // // The Firebase SDK is initialized and available here!
        //
        // firebase.auth().onAuthStateChanged(user => { });
        // firebase.database().ref('/path/to/ref').on('value', snapshot => { });
        // firebase.firestore().doc('/foo/bar').get().then(() => { });
        // firebase.functions().httpsCallable('yourFunction')().then(() => { });
        // firebase.messaging().requestPermission().then(() => { });
        // firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { });
        // firebase.analytics(); // call to activate
        // firebase.analytics().logEvent('tutorial_completed');
        // firebase.performance(); // call to activate
        //
        // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

        try {
          let app = firebase.app();
          let features = [
            'auth', 
            'database', 
            'firestore',
            'functions',
            'messaging', 
            'storage', 
            'analytics', 
            'remoteConfig',
            'performance',
          ].filter(feature => typeof app[feature] === 'function');
          loadEl.textContent = `Firebase SDK loaded with ${features.join(', ')}`;
        } catch (e) {
          console.error(e);
          loadEl.textContent = 'Error loading the Firebase SDK, check the console.';
        }
      });
    </script>
  </body>
</html>

main.dart

import 'package:deal_diligence/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:deal_diligence/screens/login_screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:deal_diligence/Services/firebase_api.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  await FirebaseApi().initNotifications();


  runApp(const ProviderScope(
      child:
          DealDiligence())); // This allows the entire app to use River_Pod for state management
}

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

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      home: LoginScreen(),
    );
  }
}

问题原因及解决方案

1. 核心问题:使用了Firebase Hosting默认index.html而非Flutter构建的页面

当前服务器上的index.html是Firebase Hosting的初始欢迎页,没有包含Flutter Web应用的入口脚本(如main.dart.js),因此永远不会加载你的Flutter应用内容,点击按钮只是循环打开同一个欢迎页。

2. 解决方案步骤

步骤1:重新构建Flutter Web应用

执行以下命令生成生产环境的Web构建文件:

flutter build web --release

构建完成后,所有Web资源会生成在项目根目录的build/web文件夹下。

步骤2:替换服务器上的文件

将build/web目录下的所有文件(包括自动生成的index.html、main.dart.js、assets等)上传到服务器的public_html目录,覆盖现有的index.html及其他文件。

步骤3:配置Firebase初始化(确保Web平台正常工作)

  • 确认firebase_options.dart包含Web平台的配置:执行flutterfire configure时,确保勾选了Web平台,生成的配置文件中应有DefaultFirebaseOptions.web相关内容。
  • 你的main.dart代码中Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform)的写法是正确的,会自动适配Web平台。

步骤4:调整base href(按需)

如果应用部署在服务器的子目录而非根目录,修改Flutter生成的index.html中的base标签:

<base href="/your-subdirectory/">

若部署在根目录,保持<base href="/">即可。

步骤5:检查服务器资源完整性

确保所有build/web中的文件都已上传,包括:

  • index.html(Flutter生成的版本)
  • main.dart.js
  • flutter.js
  • assets目录(应用资源)
  • icons目录(应用图标)

3. 验证修复

部署完成后,刷新浏览器:

  • 应该直接加载你的Flutter应用登录页面(LoginScreen)
  • 打开浏览器开发者工具(F12),检查控制台是否有Firebase初始化相关错误
  • 确认Firebase数据库连接正常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 15:24:50