title: Save Data to Firebase Realtime Database in Flutter
body: <p>In my code the user is Sign in through phone authentication.After login i want to get the user id of the user and add it to the Firebase database along with its personal details.</p>
  <p>But when i have done this the Path in database is direct rather than it should be via first UserId then under that the personal details.
  I have also provide the image with the database output.</p>
  <p>Code:</p>
  <pre><code>import 'package:firebase_auth/firebase_auth.dart';
  import 'package:firebase_database/firebase_database.dart';
  import 'package:flutter/cupertino.dart';
  import 'package:udharibook/Screens/SignInPage.dart';
  import 'package:udharibook/Screens/dashboard.dart';
  
  class AuthService  {
    String UserId ='';
    final DBRef = FirebaseDatabase.instance.reference().child('Users');
    final FirebaseAuth _auth = FirebaseAuth.instance;
    handleAuth(){
      return StreamBuilder(
        stream: FirebaseAuth.instance.onAuthStateChanged,
        builder: (BuildContext, snapshot){
          if(snapshot.hasData){
            getCurrentUser();
            writeData();
            return DashboardPage();
          }
          else {
            return SignIn();
          }
        },
      );
    }
    getCurrentUser() async{
      final FirebaseUser user = await _auth.currentUser();
      final uid = user.uid;
      // Similarly we can get email as well
      //final uemail = user.email;
      UserId = uid;
      print('User ID:  '+UserId);
  
      //print(uemail);
    }
  
    void writeData(){
      DBRef.child(UserId).set({
        'id':'ID1',
        'Name':'Mehul Jain',
        'Phone':'8856061841'
      });
    }
  
    signOut(){
      FirebaseAuth.instance.signOut();
    }
  
    signIn(AuthCredential authCreds){
      FirebaseAuth.instance.signInWithCredential(authCreds);
    }
  
    signInWithOTP(smsCode,verId){
      AuthCredential authCreds = PhoneAuthProvider.getCredential(
          verificationId: verId,
          smsCode: smsCode
      );
      signIn(authCreds);
    }
  }
   
  </code></pre>
  <p>Database Image <a href="https://i.stack.imgur.com/vjIJD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vjIJD.png" alt="enter image description here" /></a></p>