项目未连接到实时数据库-颤振



我遇到了实时数据库不工作的问题,我从GitHub复制了一个项目,并在firebase中使用我的应用程序ID创建了一个新项目,将JSON文件添加到我的应用程序中,并将writeread规则更改为true,但仍然不工作或在终端显示任何内容

源代码:

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class CustomData extends StatefulWidget {
CustomData({this.app});
final FirebaseApp app;
@override
_CustomDataState createState() => _CustomDataState();
}
class _CustomDataState extends State<CustomData> {
final refrenceDatabase = FirebaseDatabase.instance;
final MovieController  = TextEditingController();
@override
Widget build(BuildContext context) {
final ref = refrenceDatabase.reference();
return Scaffold(
appBar: AppBar(title: Text('Real Time DataBase'),backgroundColor: Colors.red,),
backgroundColor: CupertinoColors.systemGrey,
body: SingleChildScrollView(
child: Column(children: [
Center(child:
Container(
color: Colors.blue,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Column(
children: [
Text('Movie title',style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold), textAlign: TextAlign.center,),
TextField(controller: MovireController, textAlign: TextAlign.center,),
FlatButton(
color: Colors.orange,
onPressed: (){
ref.child('Movies').push().child('movie title').set(MovieController).asStream();
}, child: Text('Submit'))
],
),
),)
],),
),
);
}
}

数据库规则:

{
"rules": {
".read": true,  // 2021-3-23
".write": true,  // 2021-3-23
}
}

好的。因此,从我所看到的,您没有收到任何错误。因此,这意味着您的firebase连接正确。第二件事是代码中的以下错误。我不知道你想用下面的代码做什么,但它是不正确的。

// Firstly, you're passing a TextEditingController instead of a string.
// Secondly, you're streaming data but you're not accessing it anywhere.
ref.child('Movies').push().child('movie title').set(MovieController).asStream();
ref.child('Movies').push().set({
"movie title": MovieController.text,
});

这将把数据推送到数据库。

最新更新