What does Underscore “_” before variable name mean for Flutter

后端 未结 2 398
我在风中等你
我在风中等你 2020-12-13 23:31

with reference to the Flutter tutorial, I encountered an underscore, _.

I know that in Java, _ is used as a naming convention for a private

相关标签:
2条回答
  • 2020-12-14 00:11

    From the Dart guide

    Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore (_), it’s private to its library. For details, see Libraries and visibility.

    0 讨论(0)
  • 2020-12-14 00:15

    It's not just a naming convention. Underscore fields, classes and methods will only be available in the .dart file where they are defined.

    It is common practice to make the State implementation of a widget private, so that it can only be instantiated by the corresponding StatefulWidget:

    class MyPage extends StatefulWidget {
      @override
      _MyPageState createState() => _MyPageState();
    }
    
    class _MyPageState extends State<MyPage> {
      @override
      Widget build(BuildContext context) {
        return Container();
      }
    }
    
    0 讨论(0)
提交回复
热议问题