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

如何在Flutter Windows应用中集成Firebase?

Flutter Windows集成Firebase(身份认证+Firestore)操作步骤

前置准备

  • 在Firebase控制台新建项目,可根据需求选择是否开启Google Analytics,不开启不影响核心功能使用。
  • 给项目添加Windows应用,支持两种配置生成方式:
    1. 控制台手动添加:选择Windows平台,输入项目的Windows包名(可在Flutter项目windows/runner/Runner.rc中查找VALUE "CompanyName"和VALUE "ProductName"拼接得到,格式一般为com.yourcompany.yourappname),生成后下载firebase_options.dart放入项目lib目录
    2. 自动生成:安装flutterfire cli后,在项目根目录执行flutterfire configure,按提示选择对应Firebase项目和Windows平台,工具会自动生成配置文件。

Flutter项目依赖配置

  • 在项目根目录的pubspec.yaml文件中添加依赖:
dependencies:
  flutter:
    sdk: flutter
  firebase_core: ^最新稳定版本号
  firebase_auth: ^最新稳定版本号
  cloud_firestore: ^最新稳定版本号
  • 执行flutter pub get拉取所有依赖包。

初始化Firebase

打开项目的main.dart文件,在启动应用前完成Firebase初始化,参考代码:

import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';

void main() async {
  // 确保Widgets绑定已初始化
  WidgetsFlutterBinding.ensureInitialized();
  // 初始化Firebase
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}

注意:Flutter Windows桌面端已完成Firebase官方适配,不需要额外修改原生端配置,初始化完成即可直接调用相关API。

身份认证功能实现

  • 先进入Firebase控制台「身份认证」板块,开启你需要的登录方式,比如邮箱密码登录、手机号登录等,以邮箱密码登录为例,先开启「邮箱/密码」登录选项。
  • 注册功能示例代码:
Future<UserCredential?> registerWithEmail(String email, String password) async {
  try {
    final credential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
      email: email,
      password: password,
    );
    return credential;
  } on FirebaseAuthException catch (e) {
    debugPrint('注册错误码:${e.code},错误信息:${e.message}');
    return null;
  }
}
  • 登录功能示例代码:
Future<UserCredential?> loginWithEmail(String email, String password) async {
  try {
    final credential = await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: email,
      password: password,
    );
    return credential;
  } on FirebaseAuthException catch (e) {
    debugPrint('登录错误码:${e.code},错误信息:${e.message}');
    return null;
  }
}

Firestore数据读写实现

  • 进入Firebase控制台「Firestore Database」板块,创建数据库,开发阶段可先选择测试模式(上线前必须修改为正式安全规则)。
  • 测试阶段临时安全规则参考:
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.time < timestamp.date(2024, 12, 31);
    }
  }
}
  • 写入数据示例代码:
Future<void> addUserInfo(String uid, Map<String, dynamic> info) async {
  await FirebaseFirestore.instance.collection('users').doc(uid).set(info);
}
  • 读取数据示例代码:
Future<Map<String, dynamic>?> getUserInfo(String uid) async {
  final doc = await FirebaseFirestore.instance.collection('users').doc(uid).get();
  return doc.data();
}
  • 实时监听数据变化示例代码:
Stream<DocumentSnapshot> listenUserInfo(String uid) {
  return FirebaseFirestore.instance.collection('users').doc(uid).snapshots();
}

注意事项

  • 打包发布Windows应用前,需要在windows/runner/appxmanifest.xml中添加网络权限:<Capability Name="internetClient" />,否则打包后的应用无法连接Firebase服务。
  • 上线前必须修改Firestore安全规则,禁止使用测试模式,推荐添加身份校验逻辑,示例:allow read, write: if request.auth != null && request.auth.uid == uid;,避免数据泄露或被恶意篡改。
  • 若遇到依赖冲突问题,可执行flutter pub upgrade --major-versions更新所有依赖到兼容的最新版本。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 02:00:03