I am new to flutter and firebase and am trying to display the data of the current user that has logged in. I tried various ways myself like storing the current user email on a String variable and comparing it with they email key, but couldn't figure out how to do it.
Is there a way to get the snapshot as output of only the logged in user data?

import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:helloworld/tictactoe.dart';
import 'package:helloworld/numbergame.dart';
import 'package:helloworld/profile.dart';
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
// Remove the debug banner
debugShowCheckedModeBanner: false,
title: 'Search',
home: Search(),
);
}
}
class Search extends StatefulWidget {
const Search({Key? key}) : super(key: key);
static const String id = 'search';
@override
_SearchState createState() => _SearchState();
}
class _SearchState extends State<Search> with AutomaticKeepAliveClientMixin {
final dbRef = FirebaseDatabase.instance.ref().child("insertdata");
List<Map<dynamic, dynamic>> lists = [];
final _auth = FirebaseAuth.instance;
late String? userEmail;
void getCurrentUserEmail() async {
final user = await _auth.currentUser;
userEmail = user!.email;
}
void initState() {
super.initState();
getCurrentUserEmail();
}
@override
Widget build(BuildContext context) {
super.build(context);
//btmnavbar
return Scaffold(
resizeToAvoidBottomInset: false,
body: FutureBuilder<DatabaseEvent>(
future: dbRef.once(),
builder: (context, AsyncSnapshot<DatabaseEvent> snapshot) {
if (snapshot.hasData) {
lists.clear();
Map<dynamic, dynamic> values = snapshot.data!.snapshot.value as Map<dynamic, dynamic>;
values.forEach((key, values) {
lists.add(values);
});
return new ListView.builder(
shrinkWrap: true,
itemCount: lists.length,
itemBuilder: (BuildContext context, int index) {
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Name: " + lists[index]["Email"]),
Text("Age: " + lists[index]["First Name"]),
Text("Type: " + lists[index]["Last Name"]),
],
),
);
});
}
return CircularProgressIndicator();
}),
);
}
@override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}