user11717179
user11717179

Reputation:

Able to access private class in flutter

I found online that underscore fields, classes and methods will only be available in the .dart file where they are defined but it seems that my code goes against it:

Code sinppet from main.dart:

class _TestState extends State<Test> {
 int a = 0;
 void increase() {
   setState(() {
     a = a + 1;
   });
 }

 var b = ['hello', 'bye'];
 @override
 Widget build(BuildContext context) {
  return MaterialApp(
    home: Scaffold(
        body: Column(
      children: <Widget>[Text(b[a]), MyButton(increase)],
    )),
  );
 }
}

Code snippet from MyButton.dart:

 class MyButton extends StatelessWidget {
  final Function x;
  MyButton(this.x);
  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(onPressed: x,child:Text('press me')),
    );
  }
}

How is MyButton able to call a method from _TestState when it exists in a different file.

Upvotes: 0

Views: 1476

Answers (1)

Jhakiz
Jhakiz

Reputation: 1609

Pranav K It seems that you are seeing things differently. _TestState can call or use MyButton but not the reverse. This means your code is right; to test call _TestState in MyButton and see the result.

Upvotes: 2

Related Questions