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

求助Flutter中Firebase Database稳定依赖配置及崩溃排查

Flutter Firebase依赖兼容与实时数据库崩溃问题

我在Flutter开发中多次碰到Firebase插件互不兼容的问题,难以确定各包的兼容版本。近期添加firebase_database后,始终找不到稳定配置,导致应用使用实时连接时直接崩溃。

求分享经过验证的稳定配置,帮助排查问题根源。

当前pubspec.yaml相关配置

environment:
  sdk: ">=3.0.0"

dependencies:
  flutter:
    sdk: flutter

  firebase_core: ^2.1.0
  firebase_auth: ^4.0.0
  cloud_firestore: ^4.9.0
  firebase_database: ^10.2.7
  firebase_analytics: ^10.0.0
  cloud_functions: ^4.0.0
  #firebase_messaging will be needed eventually, but not now.

其他需求

很久以前我找到过一个直接列出Firebase各插件兼容版本的页面,但已一年多没找到,如果该页面仍存在,烦请告知,我想彻底解决Firebase依赖兼容问题。

崩溃代码片段排查

以下是导致应用崩溃的代码片段,每次监听时应用立即崩溃,似乎与_dbRef的属性有关,请求帮忙排查:

import 'package:firebase_database/firebase_database.dart';

class RealtimeService {
  final DatabaseReference _dbRef = FirebaseDatabase.instance.ref();

  String encodeFirebaseKey(String key) {
    return Uri.encodeComponent(key);
  }

  String decodeFirebaseKey(String encodedKey) {
    return Uri.decodeComponent(encodedKey);
  }

  Stream<Map<String, Conversation>> getConversationsStream(String userEmail) {

    // Check for null _dbRef
    if (_dbRef == null) {
      print('Error: _dbRef is null');
      return Stream.error('Database reference is null');
    }

    String encodedEmail = encodeFirebaseKey(userEmail);

    // Listen for values and handle potential errors inside asyncMap
    return _dbRef
        .child('userConversations')
        .child(encodedEmail)
        .onValue
        .asyncMap((event) async {
          print('mark 1');
      try {
        print('mark 2');

        Map<String, dynamic> userConvos = event.snapshot.value as Map<
            String,
            dynamic> ?? {};

        if (userConvos.isEmpty) {
          return {}; // Return empty map if no conversations found
        }

        // Fetching details of each conversation and mapping it to a list
        Map<String, Conversation> conversations = {};

        await Future.wait(userConvos.keys.map((convoId) async {
          DataSnapshot convoSnapshot = (await _dbRef.child('conversations')
              .child(convoId)
              .once()).snapshot;
          if (convoSnapshot.value != null) {
            conversations[convoId] = ConversationObject.fromMap(
                map: convoSnapshot.value as Map<String, dynamic>);
          }
        }));

        return conversations;
      } catch (e) {
        print('Error in asyncMap: $e');
        throw e;
      }
    });
  }

}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 01:40:25