Home > database >  The method 'ref' isn't defined for the type 'FirebaseDatabase'
The method 'ref' isn't defined for the type 'FirebaseDatabase'

Time:02-17

I am trying to follow the tutorial on fireflutter https://firebase.flutter.dev/docs/database/usage

The method 'ref' isn't defined for the type 'FirebaseDatabase'

What did I do wrongly? any help will be greatly appreciated

I have added the database package

import 'package:firebase_database/firebase_database.dart';

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'
}

The code is as follows:

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.ref("Location 1"); //I am having problem with this line
   // final location = database.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"});
          },
        ),
      ],
    ),
    );
  }

}

CodePudding user response:

ref isn't supported in firebase_database: ^7.0.0

Use FirebaseDatabase.instance.reference().child("Location 1");

  • Related