首页 > 解决方案 > 无法将任何数据添加到实时数据库 Firebase

问题描述

我正在尝试将数据从我的颤振应用程序发送到 firebase 实时数据库。

我的 firebase 项目已连接到我的应用程序,它已初始化,我已完成所有配置步骤和身份验证。我知道身份验证没问题,因为我在用户连接时在 firebase 中获取了用户电子邮件地址,因此应用程序已连接。我的问题是当我想使用 set() 命令将任何数据发送到实时数据库时,没有任何附加内容。

我的 pubspect.yaml 文件:

 dependencies:
  flutter:
    sdk: flutter
  firebase_auth: ^1.2.0
  google_sign_in: ^5.0.3
  firebase_database: ^6.1.2

我的 android/build.gradle 文件:

dependencies {
        
        classpath 'com.google.gms:google-services:4.3.8'
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"}

最后是我的飞镖文件:

import 'package:firebase_database/firebase_database.dart';
import "post.dart";

//First attempt using this tutorial : https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_database/firebase_database/example/lib/main.dart

void savePost(Post post) {
  // Still not working
  print("Saving post");
  FirebaseDatabase database = FirebaseDatabase();
  final test = database.reference().child("posts/");
  print("Set...");
  test.push().set(post.toJson());
  print("Set done");
}

//And second attempt from this video : https://www.youtube.com/watch?v=qWjnxjB-sTA&list=PLzMcBGfZo4-knQWGK2IC49Q_5AnQrFpzv&index=10
final databaseReference = FirebaseDatabase.instance.reference();

DatabaseReference savePost2(Post post) {
  // automatically create posts table in the database
  print("Saving post");
  var id = databaseReference.child('posts/').push();
  id.set(post.toJson());
  print("id du post : $id");
  return id;
}

我实际上为 savePost/savePost2 方法提供了一个 post 类作为参数,如果需要,这里是代码:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';


class Post {
  // We're going to create a Post class for all of out posts.
  // Here are the attributes of the class
  String body;
  String author;
  Set usersliked = {};
  Future _id;

  Post(this.body, this.author);

  void likePost(User user) {
    //print("LikePost function triggered");
    //print(usersliked);
    //print(user.uid);
    if (this.usersliked.contains(user.uid)) {
      this.usersliked.remove(user.uid);
    } else {
      this.usersliked.add(user.uid);
    }
  }

  void setId(Future id) {
    this._id = id;
  }

  // Method that transform the entire post into a dictionary in order to post it to firebase
  // Which require Json format info
  Map<String, dynamic> toJson() {
    return {
      'author': this.author,
      'usersLiked': this.usersliked.toList(),
      'body': this.body,
    };
  }
}

这是我启动应用程序、连接并发布内容时的输出控制台(在发布期间,它在发送到数据库时被感知到。)

Launching lib\main.dart on SM A520F in debug mode...
lib\main.dart:1
Format incorrect du param�tre -
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:55233/pgCCjKx58HM=/ws
D/libGLESv2( 9717): STS_GLApi : DTS, ODTC are not allowed for Package : com.example.hello_world
I/flutter ( 9717): Logged out!
D/ViewRootImpl@9241454[MainActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 522830450688} changed=false
D/ViewRootImpl@9241454[MainActivity]( 9717): ViewPostIme pointer 0
D/ViewRootImpl@9241454[MainActivity]( 9717): ViewPostIme pointer 1
I/flutter ( 9717): Starting to sign in
I/zygote64( 9717): Do partial code cache collection, code=61KB, data=51KB
I/zygote64( 9717): After code cache collection, code=61KB, data=52KB
I/zygote64( 9717): Increasing code cache capacity to 256KB
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): setView = DecorView@3585f03[SignInHubActivity] TM=true MM=false
D/ViewRootImpl@9241454[MainActivity]( 9717): MSG_WINDOW_FOCUS_CHANGED 0
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): dispatchAttachedToWindow
V/Surface ( 9717): sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): Relayout returned: old=[0,0][0,0] new=[0,0][1080,1920] result=0x7 surface={valid=true 522820235264} changed=true
D/mali_winsys( 9717): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
D/OpenGLRenderer( 9717): eglCreateWindowSurface = 0x79bb1a8100
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@dbcb42d nm : com.example.hello_world ic=null
I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): MSG_WINDOW_FOCUS_CHANGED 0
D/ViewRootImpl@9241454[MainActivity]( 9717): MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@13d3be5 nm : com.example.hello_world ic=null
I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
D/OpenGLRenderer( 9717): eglDestroySurface = 0x79bb1a8100
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
D/ViewRootImpl@6b03514[SignInHubActivity]( 9717): dispatchDetachedFromWindow
D/InputEventReceiver( 9717): channel 'c358f36 com.example.hello_world/com.google.android.gms.auth.api.signin.internal.SignInHubActivity (client)' ~ Disposing input event receiver.
I/flutter ( 9717): redirect
D/ViewRootImpl@9241454[MainActivity]( 9717): ViewPostIme pointer 0
D/ViewRootImpl@9241454[MainActivity]( 9717): ViewPostIme pointer 1
D/InputMethodManager( 9717): SSI - flag : 0 Pid : 9717 view : com.example.hello_world
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@7f3831b nm : com.example.hello_world ic=io.flutter.plugin.editing.InputConnectionAdaptor@6833cb8
I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
D/ViewRootImpl@9241454[MainActivity]( 9717): MSG_RESIZED: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 873) vi=Rect(0, 72 - 0, 873) or=1
D/ViewRootImpl@9241454[MainActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 522830450688} changed=false
D/ViewRootImpl@9241454[MainActivity]( 9717): ViewPostIme pointer 0
D/ViewRootImpl@9241454[MainActivity]( 9717): ViewPostIme pointer 1
I/flutter ( 9717): Creating a new post
I/flutter ( 9717): Saving post
I/flutter ( 9717): id du post : Instance of 'DatabaseReference'
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@6053a93 nm : com.example.hello_world ic=io.flutter.plugin.editing.InputConnectionAdaptor@8c4ead0

I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
W/IInputConnectionWrapper( 9717): getExtractedText on inactive InputConnection
D/InputMethodManager( 9717): SSI - flag : 0 Pid : 9717 view : com.example.hello_world
D/InputMethodManager( 9717): HSIFW - flag : 0 Pid : 9717
D/ViewRootImpl@9241454[MainActivity]( 9717): MSG_RESIZED: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
D/ViewRootImpl@9241454[MainActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 522830450688} changed=false
I/zygote64( 9717): Do full code cache collection, code=116KB, data=108KB
I/zygote64( 9717): After code cache collection, code=109KB, data=86KB
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@fb7690b nm : com.example.hello_world ic=null
I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
D/SurfaceView( 9717): BG show() Surface(name=Background for - SurfaceView - com.example.hello_world/com.example.hello_world.MainActivity@5cf0ef9@0) io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ......I. 0,0-1080,1920}
D/SurfaceView( 9717): surfaceDestroyed 1 #1 io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ......I. 0,0-1080,1920}
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@b629ef5 nm : com.example.hello_world ic=null
I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
D/SurfaceView( 9717): BG show() Surface(name=Background for - SurfaceView - com.example.hello_world/com.example.hello_world.MainActivity@5cf0ef9@1) io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ......I. 0,0-1080,1920}
V/Surface ( 9717): sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/SurfaceView( 9717): surfaceCreated 1 #1 io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ......I. 0,0-1080,1920}
D/mali_winsys( 9717): EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
D/SurfaceView( 9717): surfaceChanged (1080,1920) 1 #1 io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ......I. 0,0-1080,1920}
D/SurfaceView( 9717): BG destroy() Surface(name=Background for - SurfaceView - com.example.hello_world/com.example.hello_world.MainActivity@5cf0ef9@0) io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ......I. 0,0-1080,1920}
D/ViewRootImpl@9241454[MainActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x1 surface={valid=true 522830450688} changed=false
D/ViewRootImpl@9241454[MainActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x3 surface={valid=true 522830450688} changed=false
D/ViewRootImpl@9241454[MainActivity]( 9717): MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
V/InputMethodManager( 9717): Starting input: tba=android.view.inputmethod.EditorInfo@bea848a nm : com.example.hello_world ic=null
I/InputMethodManager( 9717): startInputInner - mService.startInputOrWindowGainedFocus
D/SurfaceView( 9717): BG show() Surface(name=Background for - SurfaceView - com.example.hello_world/com.example.hello_world.MainActivity@5cf0ef9@1) io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ........ 0,0-1080,1920}
D/SurfaceView( 9717): surfaceDestroyed 1 #1 io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ........ 0,0-1080,1920}
D/SurfaceView( 9717): BG show() Surface(name=Background for - SurfaceView - com.example.hello_world/com.example.hello_world.MainActivity@5cf0ef9@1) io.flutter.embedding.android.FlutterSurfaceView{5cf0ef9 V.E...... ........ 0,0-1080,1920}
D/OpenGLRenderer( 9717): eglDestroySurface = 0x79b7750140
D/ViewRootImpl@9241454[MainActivity]( 9717): Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
D/ViewRootImpl@9241454[MainActivity]( 9717): MSG_WINDOW_FOCUS_CHANGED 0

标签: androidfirebaseflutterfirebase-realtime-database

解决方案


推荐阅读