Home > Software engineering >  Dart class constructor with required arguments while not initializing formal
Dart class constructor with required arguments while not initializing formal

Time:06-01

I have the following basic class with its constructor in Dart:

class ChartData {
  String? name;
  Color? color;
  Duration? duration;
  ChartData(
      String name, List rgbo, Duration duration) {
    this.name = name;
    this.color = Color.fromRGBO(rgbo[0], rgbo[1], rgbo[2], rgbo[3]);
    this.duration = duration;
  }
}
  1. How can I make it so that the constructor arguments are required, and the class arguments are non-nullable thus don't need any null safety? I'm aware of the keyword required, but from what I understand it works only for initializing formal class constructors.

  2. How could we use initializing formal for this class, especially regarding constructing the color class argument?

CodePudding user response:

First of all, you should use an initializer list to initialize the fields, not do assignments in the constructor body. Dart is like C in that regard, not Java.

class ChartData {
  final String name;
  final Color color;
  final Duration duration;
  ChartData(String name, List<int> rgbo, Duration duration)
      : this.name = name,
        this.color = Color.fromRGBO(rgbo[0], rgbo[1], rgbo[2], rgbo[3]),
        this.duration = duration;
  
}

This change allows your fields to be final and non-nullable, because now they are initialized before they can ever be read. Your arguments are required. They already were, but they still are.

If you want to use initializing formals, and you do, you can replace an initializer list entry of the form this.name = name (or name = name, because the this is already optional) with a parameter of the form this.name:

class ChartData {
  final String name;
  final Color color;
  final Duration duration;
  ChartData(this.name, List<int> rgbo, this.duration)
      : color = Color.fromRGBO(rgbo[0], rgbo[1], rgbo[2], rgbo[3]);
}

The color parameter cannot be an initializing formal because it doesn't store the argument directly into the field. Just keep that as an initializer list entry instead.

This works, the fields are final and non-nullable, the parameters are required and non-nullable, and you use initializing formals where possible.

You asked about required. That modifier works with named parameters, and your parameters are positional. If you wanted them to be named instead, you could write it as:

class ChartData {
  final String name;
  final Color color;
  final Duration duration;
  ChartData(
      {required this.name, required List<int> rgbo, required this.duration})
      : color = Color.fromRGBO(rgbo[0], rgbo[1], rgbo[2], rgbo[3]);
}

The {...} surrounding the parameters makes them named. Required named parameters need a required in front, named parameters default to being optional. Whether you like required named parameters or not is a matter of taste. Some hate writing and reading the extra name, others prefer it because they find it easier to read. Either version works.

CodePudding user response:

  1. To set the arguments to non-null, you must add required to each argument in a constructor.

  2. If you want to initialize the arguments, you could call the class and set the values. Also you can initialize in some initState()

For example:

class ChartData {
    String name;
    Color color;
    Duration duration;

  ChartData({
    required this.name,
    required this.color,
    required this.duration
});
}

class OtherClass extends StatelessWidget {

  //initialize
  final chartData = ChartData(
      name: "name1",
      color: Color.fromRGBO(38, 38, 38, 0.4),
      duration: const Duration(seconds:15));

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
  •  Tags:  
  • dart
  • Related