I am trying to use firebase for a flutter app I'm working on and when I get to the step of actually accessing the data in the database I'm encountering this weird error: "The instance member'loggedInUser' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression"
Code:
class _MainPageState extends State<MainPage> {
User? user = FirebaseAuth.instance.currentUser;
UserModel loggedInUser = UserModel();
@override
void initState() {
super.initState();
_performSingleFetch();
}
void _performSingleFetch() {
final database = FirebaseDatabase.instance
.ref('users')
.child(user!.uid)
.once()
.then((snapshot) {
final data = Map<dynamic, dynamic>.from(
snapshot.snapshot.value! as Map<dynamic, dynamic>);
loggedInUser = UserModel.fromJson(data);
});
}
List pages = [
HomePage(username: loggedInUser.firstName),
CommunityPage(),
InfoPage()
];
The error pops up in this section:
HomePage(username: loggedInUser.firstName),
Does anyone know how to fix the error?