颤振中实时数据库项目(firebase)的日志记录中途停止

时间:2021-06-25 08:01:26

标签: android firebase flutter firebase-realtime-database

我希望我的 Flutter 应用程序读取 firebase 中的实时数据库项目,但我的控制台开始打印条目并在中间突然停止(要打印 30 多个条目)。这是我在控制台上的 dart 文件和输出:

// single_page.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';


class SinglePageApp extends StatefulWidget {
  const SinglePageApp({Key? key}) : super(key: key);

  @override
  _SinglePageAppState createState() => _SinglePageAppState();
}

class _SinglePageAppState extends State<SinglePageApp> {

  DatabaseReference _databaseReference = FirebaseDatabase.instance.reference();
  
  void printResults() {
    _databaseReference.once().then((DataSnapshot snapshot) {
      print('Data : ${snapshot.value}');
    });
  }

  @override
  Widget build(BuildContext context) {
      printResults();
      return Container();
  }
}
// main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import './single_page.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized(); // modified
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: SinglePageApp(),
    );
  }
}
Performing hot reload...
Syncing files to device sdk gphone x86...
Reloaded 0 libraries in 131ms.
I/chatty  (11266): uid=10154(com.example.firebasetest) 1.ui identical 1 line
I/flutter (11266): Data : {global: 1624522599522, readings: {-Mcx9YIBFKDmt2SG3zgs: {Temperature: 21, Humidity: 38}, -Mcx6NY-dij-Hrpzlxx5: {Temperature: 22, Humidity: 39}, -Mcx7RVECF5_Phy-HW4u: {Temperature: 22, Humidity: 38}, -Mcx5fF5e5Sv8qt7RwnN: {Temperature: 22, Humidity: 39}, -Mcx612cV63T02_kU2k1: {Temperature: 22, Humidity: 39}, -Mcx8-SsUNEXKDr9JwX8: {Temperature: 22, Humidity: 38}, -Mcx8TvgbNRO_Im5BEIv: {Temperature: 22, Humidity: 38}, -Mcx6yF5lxBhJwI_oR0n: {Temperature: 22, Humidity: 38}, -Mcx94EY0H7PhHsaihfd: {Temperature: 22, Humidity: 38}, -Mcx6jFx5A8RpiwslDyN: {Temperature: 22, Humidity: 39}, -Mcx8aOzK88q4x_HBeFw: {Temperature: 22, Humidity: 38}, -Mcx6VFGyad1i8KO1hLG: {Temperature: 22, Humidity: 39}, -Mcx7swFLs5QG1lM2nrA: {Temperature: 22, Humidity: 39}, -McxA0fRceADOuDCDX5I: {Temperature: 21, Humidity: 38}, -Mcx5RF48_PaUjkxVvgH: {Temperature: 22, Humidity: 39}, -Mcx7CTfM2Q0mTCxykCj: {Temperature: 22, Humidity: 39}, -Mcx6G1t_8m9gU1VI7Vf: {Temperature: 22, Humidity: 39}, -Mcx6bj61JAi_vWL25Hl: {Temperature: 22, Humidi

1 个答案:

答案 0 :(得分:1)

使用

debugPrint('Data : ${snapshot.value}')

代替

print('Data :${snapshot.value}');

printResults 方法中。

以下是来自 https://flutter.dev/docs/testing/code-debugging#logging 的解释:

<块引用>

如果一次输出太多,那么Android有时会丢弃一些 日志行。为了避免这种情况,请使用来自 Flutter 基础的 debugPrint() 图书馆。这是一个围绕 print 的包装器,它将输出限制为 避免被 Android 内核丢弃的级别。