Home > Back-end >  Why can't I access my variable in the widget?
Why can't I access my variable in the widget?

Time:12-03

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"),

    );
  }
}

CodePudding user response:

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"),

  • Related