Home > database >  Unable to write into Firebase Realtime Database
Unable to write into Firebase Realtime Database

Time:02-17

I have tried diff versions of firebase dependencies and enabled all Google APIs but to no avail.

The rules for my firebase for read and write are both set to true. I want to write Hello in my database, Under the reference Location 1.

My pubyaml dependencies are as follows:

dependencies:
  cloud_firestore: ^2.5.1
  cupertino_icons: ^1.0.2
  firebase_auth: ^3.1.0
  firebase_core: ^1.6.0
  firebase_database: ^7.0.0

My app build Gradle dependencies

dependencies {
    implementation platform('com.google.firebase:firebase-bom:28.4.0')
    implementation 'com.google.firebase:firebase-database'
    implementation 'com.google.firebase:firebase-database'
    implementation 'com.google.firebase:firebase-storage'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:multidex:1.0.3'
}

My code

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:trackkit/model/user_model.dart';
import 'package:trackkit/LoginSignup/home_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';


class AddItem extends StatelessWidget{
  AddItem({Key? key}) : super(key: key);
  final FirebaseDatabase database = FirebaseDatabase.instance;
  @override
  Widget build(BuildContext context){
    DatabaseReference location = FirebaseDatabase.instance.reference().child("Location 1");
    
    return Scaffold(
        body:Column(
      children: [
        TextFormField(
          decoration: const InputDecoration(
            border: UnderlineInputBorder(),
            labelText: 'Product name',
          ),
        ),
        ElevatedButton(
          child: const Text('Save'),
          onPressed: () async  {
              await location
                  .push()
                  .set({"hello"});
          },
        ),
      ],
    ),
    );
  }
}

After I pressed the 'Save' Button The console log shows :

E/flutter ( 4825): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument: LinkedHashSet len:1
E/flutter ( 4825): #0      StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:426:7)
E/flutter ( 4825): #1      StandardMessageCodec.writeValue.<anonymous closure> (package:flutter/src/services/message_codecs.dart:423:9)
E/flutter ( 4825): #2      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
E/flutter ( 4825): #3      StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:421:13)
E/flutter ( 4825): #4      StandardMethodCodec.encodeMethodCall (package:flutter/src/services/message_codecs.dart:561:18)
E/flutter ( 4825): #5      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:13)
E/flutter ( 4825): #6      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
E/flutter ( 4825): #7      MethodChannelDatabaseReference.set (package:firebase_database_platform_interface/src/method_channel/method_channel_database_reference.dart:97:42)
E/flutter ( 4825): #8      DatabaseReference.set (package:firebase_database/src/database_reference.dart:72:39)
E/flutter ( 4825): #9      AddItem.build.<anonymous closure> (package:trackkit/Screens/addnewitem.dart:30:20)
E/flutter ( 4825): #10     AddItem.build.<anonymous closure> (package:trackkit/Screens/addnewitem.dart:28:22)
E/flutter ( 4825): #11     _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
E/flutter ( 4825): #12     GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:193:24)
E/flutter ( 4825): #13     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
E/flutter ( 4825): #14     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
E/flutter ( 4825): #15     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:230:7)
E/flutter ( 4825): #16     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:558:9)
E/flutter ( 4825): #17     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:94:12)
E/flutter ( 4825): #18     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:139:9)
E/flutter ( 4825): #19     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:400:8)
E/flutter ( 4825): #20     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:137:18)
E/flutter ( 4825): #21     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:123:7)
E/flutter ( 4825): #22     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:440:19)
E/flutter ( 4825): #23     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:420:22)
E/flutter ( 4825): #24     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:278:11)
E/flutter ( 4825): #25     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
E/flutter ( 4825): #26     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
E/flutter ( 4825): #27     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
E/flutter ( 4825): #28     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
E/flutter ( 4825): #29     _rootRunUnary (dart:async/zone.dart:1444:13)
E/flutter ( 4825): #30     _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 4825): #31     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 4825): #32     _invoke1 (dart:ui/hooks.dart:185:10)
E/flutter ( 4825): #33     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:293:7)
E/flutter ( 4825): #34     _dispatchPointerDataPacket (dart:ui/hooks.dart:98:31)

I have been trying for 2 days, any help will be greatly appreciated !

CodePudding user response:

You'd need to set a key-value pair if you're trying to write with curly braces ({"msg" : "hello"}). If not, then you just need to remove the curly brace and write it as ("hello") as mentioned by puf. More details can be read in the docs.

  • Related