firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.63k stars 3.95k forks source link

FirebaseDatabase doesn't write data to realtime database #6625

Closed 0x2f0713 closed 3 years ago

0x2f0713 commented 3 years ago

Discussed in https://github.com/FirebaseExtended/flutterfire/discussions/6617

Originally posted by **0x2f0713** July 15, 2021 Hi, I want to implement FirebaseDatabase into my application but it doesn't write anything although I enable Realtime Database, init FirebaseApp Here is my `main.dart` code ```dart import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_database/firebase_database.dart'; import 'package:flutter/material.dart'; import 'package:iot4home/ui/login.dart'; import 'package:sizer/sizer.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return Sizer(builder: (context, orientation, deviceType) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. primarySwatch: Colors.blue, ), home: MyHomePage(title: 'IoT for Home'), ); }); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this.title}) : super(key: key); // This widget is the home page of your application. It is stateful, meaning // that it has a State object (defined below) that contains fields that affect // how it looks. // This class is the configuration for the state. It holds the values (in this // case the title) provided by the parent (in this case the App widget) and // used by the build method of the State. Fields in a Widget subclass are // always marked "final". final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { final Future _initialization = Firebase.initializeApp(); @override Widget build(BuildContext context) { print(Theme.of(context).platform); return FutureBuilder( // Initialize FlutterFire: future: _initialization, builder: (context, AsyncSnapshot snapshot) { // Check for errors if (snapshot.hasError) { return Text("Something went wrong"); } // Once complete, show your application if (snapshot.connectionState == ConnectionState.done) { final FirebaseDatabase database = FirebaseDatabase(app: snapshot.data); DatabaseReference _messagesRef = database.reference().child('messages'); _messagesRef.set("Hello World"); return Scaffold( appBar: AppBar( // Here we take the value from the MyHomePage object that was created by // the App.build method, and use it to set our appbar title. title: Text(widget.title), ), body: LoginUI()); } // Otherwise, show something whilst waiting for initialization to complete return Scaffold( body: Center( child: CircularProgressIndicator(), )); }, ); } } ``` Sorry for my bad English
markusaksli-nc commented 3 years ago

Hi @0x2f0713 Please take a look at the example to see how to use the plugin. https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_database/firebase_database/example/lib/main.dart Let me know if you still have any issues. Thank you

0x2f0713 commented 3 years ago

Hi @0x2f0713 Please take a look at the example to see how to use the plugin. https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_database/firebase_database/example/lib/main.dart Let me know if you still have any issues. Thank you

Thank you. I tried this code in new project, it's working fine, but not working in my project :'(

markusaksli-nc commented 3 years ago

Maybe it's a setup issue in your old project? Does it throw any errors or stacktraces?