Why can't I access my variable in the widget?

0

Issue

I’m learning flutter. I’m beginner in flutter.
I’m progressing by watching the video. At now, I’m learning the stateful widget. I identified a name variable in the stateful widget then I wanna access that name variable in the widget like this
Text("${Widget.name} count : $counter"),. But I got this error "The getter ‘name’ isn’t defined for the type ‘Widget’." How can I solve this problem?

class Counter extends StatefulWidget {

  final String name ;
  Counter(this.name);

  @override
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  
  int counter = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      
        appBar: AppBar(),
        body: Text("${Widget.name} count : $counter"),

    );
  }
}

Solution

There is just typo error, you need to use widget.variableName.

On your snippet Widget.name will be widget.name.

Text("${widget.name} count : $counter"),

Answered By – Yeasin Sheikh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More