I want to display user email and Id. I wrote a code, it was working, then for no reason it display null now.. Can someone help me please ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
import 'package:file_picker/file_picker.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:flutter_dev/reusable.dart';
class Fy extends StatefulWidget {
const Fy({super.key});
@override
State<Fy> createState() => _FyState();
}
class _FyState extends State<Fy> {
PlatformFile? pickedFile;
dynamic UserId;
dynamic UserMail;
Future Please() async{
FirebaseAuth.instance.authStateChanges().listen((User? user) {
if (user != null) {
setState(() {
UserId = user.uid;
UserMail = user.email;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
'Hello $UserMail',
style: TextStyle(color: Colors.white),
),
Text(
'$UserId',
style: TextStyle(color: Colors.white),
),
]),
),
);
}
}
CodePudding user response:
I think it's not working because no authState changes are taking place. Your user is logged in, and unless you log him out, he won't undergo any auth state changes. A simple and not as elegant solution could be to simply call the user, and then display the attributes in the UI. Something like this:
class Fy extends StatefulWidget {
const Fy({super.key});
@override
State<Fy> createState() => _FyState();
}
class _FyState extends State<Fy> {
PlatformFile? pickedFile;
user = FirebaseAuth.instance.currentUser;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
'Hello ${user.email}',
style: TextStyle(color: Colors.white),
),
Text(
'${user.uid}',
style: TextStyle(color: Colors.white),
),
]),
),
);
}
}