Flutter VoidCallback field default value in Constructor

1,106

Solution 1

Either the callback should be nullable or it must be initialized with a default value. Default arguments must be constants, so as usual, if you can't provide a constant directly, use a constant sentinel value:

class DefaultCallbackExample {
  final VoidCallback onStart;
  final VoidCallback onFinish;
  final bool isEnable;

  DefaultCallbackExample({
    VoidCallback? onStart,
    required this.onFinish,
    this.isEnable = true,
  }) : onStart = onStart ?? (() {});
}

Also note that () => {} creates an anonymous function that returns an empty Set. An anonymous function with an empty body should be just () {}. Function bodies either should use => (if possible) or curly braces, but not both.

Solution 2

Does this work for you?

Create a static method that will be the default balue of the function in your class.

class Class{
final Function function;
Class({this.function = _defaultFunction});
static _defaultFunction() {}
}

Share:
1,106
NM Naufaldo
Author by

NM Naufaldo

Updated on December 31, 2022

Comments

  • NM Naufaldo
    NM Naufaldo over 1 year

    This should be an easy answer but I do not find the solution yet on the internet.

    Let say I have 1 VoidCallback field name onStart in a constructor, and I want this field to have a default value when there is no parameter pass it to the constructor. I already try some code but the IDE gives me a warning. How to provide this default value?

    class DefaultCallbackExample {
      final VoidCallback onStart;
      final VoidCallback onFinish;
      final bool isEnable;
    
      DefaultCallbackExample({
        this.onStart = (() => {}), // IDE Warning
        required this.onFinish,
        this.isEnable = true,
      });
    }
    
    class DefaultCallbackExample {
      final VoidCallback onStart;
      final VoidCallback onFinish;
      final bool isEnable;
    
      DefaultCallbackExample({
        this.onStart = const (() => {}), // IDE Warning
        required this.onFinish,
        this.isEnable = true,
      });
    }
    

    There is a solution from jamesdin, but I hope there is a simpler solution not to have to write constant sentinel value. Maybe in the future dart version, there is a possible solution

    • Andrej
      Andrej over 2 years
      Why did you wrap ( ) => in ( )?
    • NM Naufaldo
      NM Naufaldo over 2 years
      If I'm not wrapping it, it still not working
    • Andrej
      Andrej over 2 years
      You should let it be null, and then where you want to call it: onStart?.call(); This way you won’t get an exception when no value is passed in.
    • NM Naufaldo
      NM Naufaldo over 2 years
      Yes, I can do that, but I don't want it to be null.
  • NM Naufaldo
    NM Naufaldo over 2 years
    This solution work, but I think it's a little bit ugly.
  • NM Naufaldo
    NM Naufaldo over 2 years
    Yes I know this solution, but I hope there is a simple similar like this code this.onStart = const (() => {})
  • jamesdlin
    jamesdlin over 2 years
    If you're unwilling to create a named function, then no. You can't use const with a lambda, and Function is an abstract class without a const constructor.
  • jamesdlin
    jamesdlin over 2 years