'Dart: Constructor with named parameters and executing a function

I would like to create a class with named parameters which also executes a function. However, I am not able to find a soluiton to call a function from the constructor. Is there something like a default init function?

My main goal is to trigger the init function as soon as the class is being createtd in order to show a toast notification. Does anyone knows how I can archive that?

The package I am using: https://pub.dev/packages/fluttertoast

This is my code:

class CustomToast {
  CustomToast({required this.message});

  final String message;
  late FToast fToast;

  void init() {
    fToast = FToast();
    fToast.init(globalKey.currentState!.context);
    _showToast();
  }

  _showToast() {
    fToast.showToast(
      child: toast,
      gravity: ToastGravity.BOTTOM,
      toastDuration: const Duration(seconds: 1),
    );
  }

  Widget toast = Container(...);
}

Kind regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source