'How can I Display currentUsers -> Name. Using RealTime Database
I want to Display logged user's username. I am using the firebase realTime database, not the cloud firestore.
I am looking at different methods like Stream Builder, Datasnap shot. Anything that works is welcomed
HERE IS MY CODE:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../services/auth.dart';
import 'home.dart';
import 'settings.dart';
import 'package:shadow_app_project/data_models/user_profile_browse.dart';
import 'package:shadow_app_project/image_selection/user_edit_image.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';
class SettingsUI extends StatelessWidget {
const SettingsUI({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: "Setting UI",
home: EditProfilePage(),
);
}
}
class EditProfilePage extends StatefulWidget {
const EditProfilePage({Key? key}) : super(key: key);
@override
_EditProfilePageState createState() => _EditProfilePageState();
}
class _EditProfilePageState extends State<EditProfilePage> {
TextEditingController displayNameController = TextEditingController();
TextEditingController ageController = TextEditingController();
bool isLoading = false;
User? user;
UserProfileBrowse? userModel;
String? imageUrl;
final refDatabase = FirebaseDatabase.instance;
bool showPassword = false;
final userName = "Name";
final userAge = "age";
String name = FirebaseDatabase.instance.ref().child('userProfileBrowse/${FirebaseAuth.instance.currentUser?.uid}/name').toString();
var usersRef = FirebaseDatabase.instance
.ref()
.child('userProfileBrowse/${FirebaseAuth.instance.currentUser?.uid}');
String currentUserEmail = (Auth().auth.currentUser as User).email.toString();
@override
Widget build(BuildContext context) {
final ref = refDatabase.ref();
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
elevation: 1,
leading: IconButton(
icon: const Icon(
Icons.arrow_back,
color: Colors.green,
),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => const SettingsPage()));
},
),
actions: [
IconButton(
icon: const Icon(
Icons.settings,
color: Colors.green,
),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => const SettingsPage()));
},
),
],
),
body: StreamBuilder(
stream: usersRef.onValue,
builder: (context, snapshot) {
return Container(
padding: const EdgeInsets.only(left: 16, top: 25, right: 16),
child: GestureDetector(
onTap: () {
FocusScope.of(context).unfocus();
},
child: ListView(
children: [
const Text(
"Edit Profile",
style: TextStyle(fontSize: 25, fontWeight: FontWeight.w500),
),
const SizedBox(
height: 15,
),
Container(
width: 130,
height: 130,
decoration: BoxDecoration(
border: Border.all(
width: 4,
color: Theme.of(context).scaffoldBackgroundColor),
boxShadow: [
BoxShadow(
spreadRadius: 2,
blurRadius: 10,
color: Colors.black.withOpacity(0.1),
offset: const Offset(0, 10))
],
shape: BoxShape.circle,
image: const DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
"https://images.pexels.com/photos/3307758/pexels-photo-3307758.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=250",
))),
),
const SizedBox(
height: 35,
),
Text(userName,
textAlign: TextAlign.left,
style: const TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
TextFormField(
// initialValue: name,
controller: displayNameController,
keyboardType: TextInputType.name,
),
Text(userAge,
textAlign: TextAlign.left,
style: const TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
TextFormField(
// initialValue: ,
controller: ageController,
keyboardType: TextInputType.number,
),
const Padding(
padding: EdgeInsets.all(8.0),
child: Text("Email: ", style: TextStyle(fontSize: 20),),
),
Text(currentUserEmail,
textAlign: TextAlign.left,
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
const SizedBox(
height: 35,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
onPressed: () {},
child: const Text("CANCEL",
style: TextStyle(
fontSize: 14,
letterSpacing: 2.2,
color: Colors.black)),
),
TextButton(
onPressed: () {
ref.child('userProfileBrowse/${FirebaseAuth.instance.currentUser?.uid}/name').set(displayNameController.text);
print("updating name");
ref.child('userProfileBrowse/${FirebaseAuth.instance.currentUser?.uid}/age').set(ageController.text);
print("updating age");
},
child: const Text(
"SAVE",
style: TextStyle(
fontSize: 14,
letterSpacing: 2.2,
color: Colors.white),
),
)
],
)
],
),
),
);
}
),
);
}
}
Should I even stream builder in my code? I just want to display my logged user's name in my TextFormField initialvalue: name,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
