'How to ensure user does not pass null variable to Flutter plugin?

Let's say, in Dart/Flutter you are designing a plugin API have a function like this:

static Future<void> startPlaying(Uint8List bytes) async {
    _channel.invokeMethod('playBytes', bytes);
}

Would it be true to say that, thanks to the null-safety feature of Dart, we are not responsible in the event that the 'bytes' parameter is passed in as null by the plugin user (and thus causing a runtime crash) because in order to do so, they would have to explicitly unwrap a nullable variable, thereby promising that it is is not null.

OR... is there some other thing we should do such as throwing an exception?



Solution 1:[1]

First:

if you mean the final user that use app on device, you should have a validation for input.

Second:

if you mean someone try your plugin to code the app. you can add required keyword, the dart analysis give the user the error if don't pass bytes.

static Future<void> startPlaying({required Uint8List bytes}) async {
    _channel.invokeMethod('playBytes', bytes);
}

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Hadi