I want to import information retrieved from FirebaseAuth into a Text Widget and display it.
I'm new to Flutter and don't know much about it, so I'd like to know more.
By the way, I was able to get the information and display it in Print.
This is the code for the retrieving side.
void initState(){
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user != null) {
String? name = user.displayName; // <-- User ID
String? email = user.email; // <-- Their email
String? image = user.photoURL;
String? uid = user.uid;
print(name);
print(email);
print(uid);
print(image);
}
});
}
This is the code for the side you want to display
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => account()),
);
},
child:
UserAccountsDrawerHeader(
accountName:Text('name'),//I want it to appear here
accountEmail: Text('email',style: TextStyle(color: Colors.white),),//I want it to appear here
currentAccountPicture: CircleAvatar(
child: ClipOval(
child: Image.network(
'https://pbs.twimg.com/profile_images/1494938183448281089/xXIv3xmE_400x400.jpg',
width: 90,
height: 90,
fit: BoxFit.cover,
),
),
),
decoration: BoxDecoration(
color: Colors.lightGreen,
image: DecorationImage(
image: NetworkImage(
'https://pbs.twimg.com/profile_banners/1394312681209749510/1634787753/1500x500',
),
fit: BoxFit.cover,
),
),
),
),
ListTile(
leading: Icon(Icons.event_available_outlined),
title: Text('行事予定'),
onTap: () {
launch('https://www.ous.ac.jp/common/files//285/20220311164731084854.pdf');
},
),
ListTile(
leading: Icon(Icons.public_outlined),
title: Text('マイログ'),
onTap: () {
launchUrl(Uri.https('mylog.pub.ous.ac.jp', '/uprx/up/pk/pky501/Pky50101.xhtml'),mode:LaunchMode.externalApplication );
},
),
ListTile(
leading: Icon(Icons.book_outlined),
title: Text('学生便覧'),
onTap: () {
launch('https://edu.career-tasu.jp/p/digital_pamph/frame.aspx?id=7540000-3-30&FL=0');
},
),
Divider(),
ListTile(
leading: Icon(Icons.link_outlined),
title: Text('各種リンク集'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Link()),
);
},
),
ListTile(
leading: Icon(Icons.call_outlined),
title: Text('各種連絡先'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Call()),
);
},
),
Divider(),
ListTile(
leading: Icon(Icons.settings_outlined),
title: Text('設定/その他'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Setting()),
);
},
),
],
),
);
}
}
CodePudding user response:
You can declare the fields you want to display as members of your state class:
class YourWidgetState extends State<YourWidget> {
String? name;
String? email;
String? image;
String? uid;
// ...
Then in your initState()
method, set those values rather than create new variables:
void initState(){
FirebaseAuth.instance
.authStateChanges()
.listen((User? user) {
if (user != null) {
// here, don't declare new variables, set the members instead
setState(() {
name = user.displayName; // <-- User ID
email = user.email; // <-- Their email
image = user.photoURL;
uid = user.uid;
});
}
});
}
And then when you want to display the values, you can create Text
widgets with them:
@override
Widget build(BuildContext context) {
// ...
Text(name ?? 'default text if name is null')
// similar for the other values you want to diplay
}