简体   繁体   中英

Unable to write into Firebase database

I am unable to write data into the real-time database. I have added the following dependencies inside the pubspec:yaml Edit: I have added the console log

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
  flutter:
    sdk: flutter
  fluttertoast: ^8.0.8

I have also added the correct google.json file. ( I have done the authentication ). Firebase database rules for read and write are both set to true.

My code is as follows:


class AddItem extends StatelessWidget{
  AddItem({Key? key}) : super(key: key);
   final database = FirebaseDatabase.instance.reference();
  @override
  Widget build(BuildContext context){
    final location = database.child('/S2.2');
    
    return Scaffold(
        body:Column(
      children: [
        TextFormField(
          decoration: const InputDecoration(
            border: UnderlineInputBorder(),
            labelText: 'Product name',
          ),
        ),
        TextFormField(
          decoration: const InputDecoration(
            border: UnderlineInputBorder(),
            labelText: 'Expiration Date',
          ),
        ),
        TextFormField(
          decoration: const InputDecoration(
            border: UnderlineInputBorder(),
            labelText: 'Current Date',
          ),
        ),
        TextFormField(
          decoration: const InputDecoration(
            border: UnderlineInputBorder(),
            labelText: 'Quantity',
          ),
        ),
        ElevatedButton(
          child: const Text('Save'),
          onPressed: () async {
              location.set({'hi'
              });

          },
        ),

      ],

    ),
    );
  }

}

THe console reads:

W/Firestore( 6723): (24.0.1) [WriteStream]: (6db7a1c) Stream closed with status: Status{code=PERMISSION_DENIED, description=Cloud Firestore API has not been used in project 966701182638 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firestore.googleapis.com/overview?project=966701182638 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., cause=null}.

Edit:

I have enabled the APIs. and the console no longer shows any errors. I am using the Firebase Realtime Database. Once I click the 'Save button', a child of 'S2.2' should form from the root, followed by another child of 'hi'. What's wrong?

Edit 2:

After pressing the save button, which sets the data in the database. The console reads:

E/flutter (19417): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Invalid argument: LinkedHashSet len:1

The console also reads


 DatabaseReference.set (package:firebase_database/src/database_reference.dart:72:39)

Any help is greatly appreicated

It seems the Firestore API in your project is completely disabled, which means you can't access it from within your Flutter app or anywhere else.

If you click the link that is in the error message, it takes you to a place in the Google Cloud console where you can enable it. If you don't have permission on the project do this, you'll have to reach out to one of the project administrators.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM