Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Create Static Members

This video was last updated on Jun 28 2022

Understand how static members work on your Dart objects by defining class based constants.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

When you define a class, you define methods and properties for that class. Each instance of the class maintains its own state. This is useful, but there are times that you want to keep track of items on a class level.

class User {
  final int id;
  final String name;
  
  User(this.id, this.name); 
  User.anonymous() : id = 0, name = 'anonymous';
}

User.maxAttribute
static const anonymousUserId = 0;
static const anonymousUserName = 'anonymous';
User.anonymous() : id = anonymousUserId, name = anonymousUserName;
void main() {
  print(User.anonymousUserId);
}
private static const anonymousUserId = 0;
static const _anonymousUserId = 0;
static const _anonymousUserName = 'anonymous';
print(User._anonymousUserId);
import 'dart:convert';
static String encode(User user) {

}
print(name);
final encodedString = utf8.encode(user.name);
final base64String = base64.encode(encodedString);
return base64String;
User.encode();
User.encode(user)
print(User.encode(user));

Reviews

Comments