Home > Back-end >  How to initialise an object of a class which requires a dictionary pattern in Flutter?
How to initialise an object of a class which requires a dictionary pattern in Flutter?

Time:08-20

class StorageItem {
  String key;
  int? size;
  DateTime? lastModified;
  String? eTag;

  StorageItem({required this.key, this.size, this.lastModified, this.eTag});
}

I want to create an object of this class and initialise it. What's the way to do it?

StorageItem item;

CodePudding user response:

Since key is required, then you at least need:

final item = StorageItem(key: 'yourKeyString');

Then you can add and pick different number of arguments:

final item = StorageItem(key: 'yourKeyString', size: 1337);
final item = StorageItem(key: 'yourKeyString', size: 1337, lastModified: DateTime.now());
final item = StorageItem(key: 'yourKeyString', size: 1337, lastModified: DateTime.now(), eTag: 'yourETagString');

CodePudding user response:

Only key is required in this class, you can do it like

StorageItem item = StorageItem(key: "your key");

StorageItem item2 = StorageItem(key: "your key",eTag: "tag");

You can check more about instance-variables

  • Related