0

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?

  • you cannot do that thing as you are processing things in initstate. So when ever you initialize a variable that is before initstate. what you can do is declare the List pages as empty and then you can add the pages in the list in _performSingleFetch method or after completing that method which should give you the user – Sagar Acharya Mar 14 '22 at 06:37

1 Answers1

0

You can't use any instance member(class attributes) for the initialization of any other instance member. So you need to declare pages as late List and inside initState you can initialize it. Here loggedInUser is an instance member and pages as well that is why you are getting the error. Update your code like the below one:-

class _MainPageState extends State<MainPage> {
  User? user = FirebaseAuth.instance.currentUser;
  UserModel loggedInUser = UserModel();
  late List<Widget> pages;

  @override
  void initState() {
    super.initState();

    pages = [];

    _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);

    pages = [
      HomePage(username: loggedInUser.firstName??''),//you can have any default name here
      CommunityPage(),
      InfoPage()
    ];

    });
  }
  • Thank you! This fixed that initial error but now it's giving a new one any time I try to interact with the app section that uses pages `LateError (LateInitializationError: Field 'pages' has not been initialized.)`. Any recommendations? – proxence Mar 14 '22 at 13:29
  • You could initialize it before using it like pages = []. And also declare it as List pages; – Ankit Kumar Maurya Mar 14 '22 at 14:07
  • Thank you, that fixed it! For some reason, it's now saying that the `loggedInUser.firstName` value is null, though. It's popping that up on the stateless widget for a component on the HomePage(). This is the section it shows up on `BoldText(text: "Welcome, " + user! + "!", color: textBlack),` and the error is `_CastError (Null check operator used on a null value)`. – proxence Mar 14 '22 at 14:32
  • maybe the firstName is really null. To solve it use null check. I have update the code accordingly – Ankit Kumar Maurya Mar 14 '22 at 14:38