2

I have this screen which displays user profile:

class DisplayProfile extends StatefulWidget { final UserProfile profile; // profile contains name and email const DisplayProfile({Key? key, required this.profile}) : super(key: key); @override _DisplayProfileState createState() { return _DisplayProfileState(); } } class _DisplayProfileState extends State<DisplayProfile> { Widget profileWidget() { return SingleChildScrollView( child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: < Widget>[ Padding( padding: EdgeInsets.all(4.0), child: Text("Name", style: TextStyle(fontWeight: FontWeight.bold))), Padding( padding: EdgeInsets.all(4.0), child: Text(profile.name, style: TextStyle(fontWeight: FontWeight.bold))), Padding( padding: EdgeInsets.all(4.0), child: Text("E-mail", style: TextStyle(fontWeight: FontWeight.bold))), Padding( padding: EdgeInsets.all(4.0), child: Text(profile.email, style: TextStyle(fontWeight: FontWeight.bold))), ])); } @override Widget build(BuildContext context) { return SafeArea( top: false, child: Scaffold( .... ) ) } } 

Of course, the code above won't compile. Flutter complains:

The getter 'name' isn't defined for the type '_DisplayProfileState'.

How to make profile in DisplayProfile accessible from _DisplayProfileState?

2
  • does my answer work for you? Commented Sep 8, 2021 at 15:03
  • Yes it works. I replaced TextField with TextFormField, then problem is solved :) Commented Sep 9, 2021 at 4:14

1 Answer 1

3

Add widget before profile as follows:

 Padding( padding: EdgeInsets.all(4.0), child: Text(widget.profile.name, style: TextStyle(fontWeight: FontWeight.bold))), Padding( padding: EdgeInsets.all(4.0), child: Text("E-mail", style: TextStyle(fontWeight: FontWeight.bold))), Padding( padding: EdgeInsets.all(4.0), child: Text(widget.profile.email, style: TextStyle(fontWeight: FontWeight.bold))), 
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry. I copied paste the wrong section. I actually use TextField instead of Text, like this: ` Padding( padding: EdgeInsets.all(4.0), child: TextField( decoration: const InputDecoration( hintText: widget.profile.name, border: OutlineInputBorder(), )))`. Flutter complains: invalid constant value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.