Instance member 'user' can't be accessed in an initializer

17,725 views
Skip to first unread message

Alvaro Gonzalez Rico

unread,
Apr 27, 2021, 10:28:07 AM4/27/21
to Flutter Development (flutter-dev)
Hello, I am trying to pass the user.uid from the NavigationPage to ProfilePage, but I get this error message: "The instance member 'user' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression". I am using the following code: 

class NavScreen extends StatefulWidget {
  @override
  _NavScreenState createState() => _NavScreenState();
}
class _NavScreenState extends State<NavScreen> {
  final user = FirebaseAuth.instance.currentUser;
  final List<Widget> _screens = [
    ProfileScreen(profileId: user.uid),
  ];
}

Any ideas on a possible solution? Thanks

Suzuki Tomohiro

unread,
Apr 27, 2021, 10:34:07 AM4/27/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)
How about “late final List<Widget> ...”?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/03ad63f2-d56f-456e-99c3-dc0b21800601n%40googlegroups.com.

Alvaro Gonzalez Rico

unread,
Apr 27, 2021, 10:47:50 AM4/27/21
to Flutter Development (flutter-dev)
Hello! With “late final List<Widget> ...” the problem mentioned above gets resolved, but "late" is underlined in red with the following error: "Unexpected text 'late'. Try removing the text."

Suzuki Tomohiro

unread,
Apr 27, 2021, 12:32:22 PM4/27/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)
The “late” keyword is available after Dart 2.12 (= Flutter 2.0)

Alvaro Gonzalez Rico

unread,
Apr 27, 2021, 1:03:33 PM4/27/21
to Flutter Development (flutter-dev)
Hello! I have Dart version 2.12.2 and Flutter version 2.0.4. Is the "late" keyword only available with a certain import?

Alvaro Gonzalez Rico

unread,
Apr 28, 2021, 9:29:32 AM4/28/21
to Flutter Development (flutter-dev)
Hello! Do you have any recommendations? Thanks

Suzuki Tomohiro

unread,
Apr 28, 2021, 9:36:18 AM4/28/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)
I don’t know. Can you search why it doesn't work for you?


Alvaro Gonzalez Rico

unread,
Apr 28, 2021, 10:11:50 AM4/28/21
to Flutter Development (flutter-dev)
According to (https://blog.gskinner.com/archives/2021/03/flutter-lazy-instantiation-with-the-late-keyword.html#:~:text=late%20has%20another%20great%20application,is%20referenced%20the%20first%20time.) it does not work for widget parameters. Look at "What about widget parameters?" section at bottom of page. If this is the case, what should I do?

Suzuki Tomohiro

unread,
Apr 28, 2021, 10:29:00 AM4/28/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)
Is  _screens a widget parameter now? I saw it wasn’t a parameter in a constructor.

Alvaro Gonzalez Rico

unread,
Apr 28, 2021, 10:39:57 AM4/28/21
to Flutter Development (flutter-dev)
I mean I have not modified anything. The ProfileScreen is still a widget. Maybe the problem is because _screens is a List of Widgets?
late final List<Widget> _screens = [
    ProfileScreen(profileId: user.uid),
  ];

Suzuki Tomohiro

unread,
Apr 30, 2021, 9:44:59 AM4/30/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)

Alvaro Gonzalez Rico

unread,
May 3, 2021, 8:26:20 AM5/3/21
to Flutter Development (flutter-dev)
I will work on it and let you know. Thanks

Alvaro Gonzalez Rico

unread,
May 20, 2021, 10:11:45 AM5/20/21
to Flutter Development (flutter-dev)
Hello, it has been a while. I have worked on the example from the site you gave me, and the late works still does NOT work. What should I do? Thanks

On Friday, April 30, 2021 at 9:44:59 AM UTC-4 suz...@gmail.com wrote:

Suzuki Tomohiro

unread,
May 20, 2021, 10:24:39 AM5/20/21
to Flutter Development (flutter-dev)
> still does NOT work

Can you explain why it doesn’t work?

Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
0 new messages