App not reading or writing to Firebase Real time database Flutter

Viewed 36

I have been trying to read and write data into firebase in flutter android. I used to have it working with a different app 2 years back but now it doesnt work

I have followed every guidelines for authenticating to firebase

This is my file structure

enter image description here

I have added google-services.json and everything just like how its specified in SDK instructions while creating Firebase android app in FIrebase project.

This is my app level build.gradle file

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new FileNotFoundException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 31

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.crud_sqlite_app"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//    implementation platform('com.google.firebase:firebase-bom:30.4.1')
    implementation 'com.google.firebase:firebase-auth-ktx:21.0.8'


}

and this is my project level build.gradle file

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        jcenter()
        mavenCentral()  // Maven Central repository
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.13'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()  // Maven Central repository
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Please help me. I am not able to read or write to firebase using the below code


import 'package:firebase_database/firebase_database.dart';
class HomeScreen extends StatefulWidget {

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {

  DatabaseReference ref = FirebaseDatabase.instance.ref("users");

  @override
  void initState(){
    super.initState();
    _updateInviteeList();
  }

  _updateInviteeList() async{


    print("Setting");
    await ref.set({
      "name": "John",
      "age": 18,
      "address": {
        "line1": "100 Mountain View"
      }
    });
    print("Set complete");


  }

  Widget _buildNote(Invitee invitee) {
    print(invitee.id);
     .........
     .....
     .....

I do not know what is going wrong. The _updateToFirebase() is called in initState() of my home screen. I know the above function is async and i am not using await to call it but atleast it should write the map to the firebase.

Please help me.

0 Answers
Related