What does Underscore "_" before variable name mean for Flutter

Private fields also have the advantage that Lint can identify which fields were declared/instantiated and not used, which helps identify human errors.

If you declare a public field, the field may be accessed by classes outside the class in the future and so Lint cannot warn you if you added the field by mistake.


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.


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();
  }
}

Tags:

Dart

Flutter