Search code examples
flutterdart

What would a good doc comment on a dart constructor look like?


One of the dart lint rules is to document all public members, and of those constructors are included. Taking this statelesswidget as an example, what would be a doc comment for the constructor? All the parameters are the fields in the class, so I'm not sure what information should be conveyed in the constructor

class PQButton extends StatelessWidget {
/// What comment should go here?
  const PQButton({
    super.key,
    required this.text,
    this.backgroundColor = brandYellow,
    this.textColor = brandBlack,
    this.textStyle = const TextStyle(
      color: brandBlack,
    ),
    this.onPressed,
  });

  final VoidCallback? onPressed;
  final String text;
  final Color backgroundColor;
  final Color textColor;
  final TextStyle textStyle;
}

Solution

  • If you want to make good, concise & easy to read documentation just look at native classes like this MaterialApp class documentation of constructor:

    class MaterialApp extends StatefulWidget {
      /// Creates a MaterialApp.
      ///
      /// At least one of [home], [routes], [onGenerateRoute], or [builder] must be
      /// non-null. If only [routes] is given, it must include an entry for the
      /// [Navigator.defaultRouteName] (`/`), since that is the route used when the
      /// application is launched with an intent that specifies an otherwise
      /// unsupported route.
      ///
      /// This class creates an instance of [WidgetsApp].
      ///
      /// The boolean arguments, [routes], and [navigatorObservers], must not be null.
      const MaterialApp({
    

    Or use plugins like MintLify or Copilot (but always fact check & add information that seems useful to you). Golden rule is- write down everything that you think is important. If you'd have to explain that code to someone, what would you say to them.