Flutter Community
Published in

Flutter Community

Dart type you have NEVER used

Dart type system goes: never say never…

Table of Contents

  • Dart type system before and after null safety
  • When should I use “Never” in my code?
  • Your contribution

Dart type system before and after null safety

Dart type system before (left) and after (right) null safety

When should I use “Never” in my code?

I do adore how this type is described in the documentation:

// User data class
class User {
final String email;
final String password;
const User({
required this.email,
required this.password,
});
@override
String toString() => 'User {email: $email, password: $password}';
}
// Helper function for required field validation
Never isRequired(String property) {
throw ArgumentError('$property is required.');
}
void main() {
String? email;
String? password;
// ...
// Some code to get email and password
// ...
if (email == null || email.isEmpty) isRequired('email');
if (password == null || password.isEmpty) isRequired('password');
final user = User(email: email, password: password); // ...
// Do something with the user data
// ...
}

Your contribution

👏 Press the clap button below to show your support and motivate me to write better!
💬 Leave a response to this article by providing your insights, comments or requests for future articles.
📢 Share this article with your friends, colleagues on social media.
➕ Follow me on Medium and check other articles.
🍿 Subscribe to my YouTube channel — more content is coming soon!
✉️ Do not hesitate to DM me on Twitter and share your opinion about… anything!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store