使用实时数据库时未上传数据

时间:2021-06-16 17:42:04

标签: firebase flutter firebase-realtime-database

我正在尝试使用实时数据库,我按照以下步骤操作,但数据未上传,调试控制台中未显示错误

  1. 在 Firebase 控制台上创建了一个项目
  2. 已注册应用(未添加 SHA 密钥)
  3. 在我的项目中添加了 google.json
  4. 在测试模式下创建新的实时数据库

这是我的代码

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

void main() {
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    routes: {
      '/': (context) => const HomeScreen(),
    },
    initialRoute: '/',
  ));
}

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

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

class _HomeScreenState extends State<HomeScreen> {
  TextEditingController sendchat = TextEditingController();
    final databaseReference =
        FirebaseDatabase.instance.reference(); //Reference to connect with DB
    final dbname = 'userchat';

  var textInputDecoration = const InputDecoration(
    labelStyle: TextStyle(
        color: Colors.white,
        letterSpacing: 1,
        fontSize: 22.0,
        fontWeight: FontWeight.bold),
    filled: true,
    fillColor: Colors.white,
    enabledBorder: OutlineInputBorder(
      borderRadius: BorderRadius.all(Radius.circular(12.0)),
    ),
    focusedBorder: OutlineInputBorder(
        borderSide: BorderSide(
      color: Colors.blue,
      width: 2.0,
    )),
  );

  @override
  Widget build(BuildContext context) {


    print('dbreference ${databaseReference.path}');
    print('dbreference1 $databaseReference');

    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: const Text('Realtime DB Demo'),
      ),
      bottomSheet: Padding(
        padding: const EdgeInsets.only(left: 5, right: 0, bottom: 5),
        child: Row(
          children: [
            Container(
              width: MediaQuery.of(context).size.width * 0.80,
              child: TextField(
                textAlign: TextAlign.left,
                controller: sendchat,
                keyboardType: TextInputType.text,
                decoration: InputDecoration(
                  hintText: 'Add message',
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(30),
                    borderSide: const BorderSide(
                      width: 1,
                      style: BorderStyle.none,
                    ),
                  ),
                  filled: true,
                  contentPadding: const EdgeInsets.all(16),
                  fillColor: Colors.white,
                ),
              ),
            ),
            const SizedBox(
              width: 10,
            ),
            Container(
                decoration: BoxDecoration(
                  color: Colors.teal[900],
                  shape: BoxShape.circle,
                ),
                child: IconButton(
                    onPressed: () {
                      print('pressed ${sendchat.text}');
                      databaseReference.child(dbname).set({
                        'message': sendchat.text,
                        'value': 'Test',
                      });
                    },
                    icon: const Icon(Icons.send))),
          ],
        ),
      ),
    );
  }
}



print('dbreference ${databaseReference.path}'); // No value is displayed
print('dbreference1 $databaseReference');  // Value displayed: Instance of 'DatabaseReference'

我的代码或设置是否有问题? .

0 个答案:

没有答案