Espanola
Espanola

Reputation: 67

Flutter: Can't access 'this' in a field initializer to read 'msgTextStyle', when trying to refer to a variable from another variable

I got error when trying to refer to a variable from another variable. In below code, error happen when refer to msgTextStyle in msgList. Error: "Can't access 'this' in a field initializer to read 'msgTextStyle'." How to fix this while still keep the my intention, and what is the best practice? Thank you!

    final msgTextStyle = TextStyle(
        fontFamily: 'Roboto',
        fontSize: 18,
        letterSpacing: 0.5,
        height: 2,
    );

    final msgList = Column( children: [
        Text('text 1.',style: msgTextStyle),
        Text('sync: added 4 numbers into call blacklist.',style: msgTextStyle)
    ]);

Upvotes: 1

Views: 3960

Answers (1)

Nae
Nae

Reputation: 15335

It seems like you're trying to access an instance variable (msgTextStyle), in another instance variable(msgList) before constructor is initialized. This is disallowed as you're trying to access something that isn't. Move the initialization of msgList to constructor or initState or perhaps try converting msgTextStyle to const.

Upvotes: 2

Related Questions