set static method

Future<bool> set({
  1. required String key,
  2. required String value,
  3. Type type = Type.CRED_TYPE_GENERIC,
  4. Persist persist = Persist.CRED_PERSIST_LOCAL_MACHINE,
  5. String userName = 'com.ayoub.flutter_windows_vault',
  6. bool encrypted = false,
  7. bool fAsSelf = false,
})

save a key/value data in windows credential manager (vault). you could find more info: https://docs.microsoft.com/en-us/windows/win32/api/wincred/nf-wincred-credwritew

handle errors: https://docs.microsoft.com/en-us/windows/win32/api/wincred/nf-wincred-credwritew#return-value

FlutterWindowsVault.set(

// key (TargetName): The name of the credential. The TargetName and Type members uniquely identify the credential.
// This member cannot be changed after the credential is created.
// Instead, the credential with the old name should be deleted and the credential with the new name created.
key: 'password',

// value (CredentialBlob): Secret data for the credential. The CredentialBlob member can be both read and written.
value: '123456789',

// type: The type of the credential. This member cannot be changed after the credential is created. The values are in Type enum.
// default value is Type.CRED_TYPE_GENERIC.
// for more about the types => https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala#members
type: Type.CRED_TYPE_GENERIC,

// persist: Defines the persistence of this credential. This member can be read and written. the values are in Persist enum.
// for more about the types => https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala#members
// default value is Persist.CRED_PERSIST_LOCAL_MACHINE.
persist: Persist.CRED_PERSIST_LOCAL_MACHINE,

// userName: The user name of the account used to connect to key (TargetName).
// default value is 'com.ayoub.flutter_windows_vault'.
userName: 'com.example.<your_app_name>',

// encrypted: whether to encrypt the data before saving it or not.
// default value is false.
encrypted: true,

// fAsSelf: (this works only if encrypted is true) Set to TRUE to specify that the credentials are encrypted in the security context of the current process.
// Set to FALSE to specify that credentials are encrypted in the security context of the calling thread security context.
// default value is false.
fAsSelf: false,

you could find more https://docs.microsoft.com/en-us/windows/win32/api/wincred/ns-wincred-credentiala#members

Implementation

static Future<bool> set({
  required String key,
  required String value,
  Type type = Type.CRED_TYPE_GENERIC,
  Persist persist = Persist.CRED_PERSIST_LOCAL_MACHINE,
  String userName = 'com.ayoub.flutter_windows_vault',
  bool encrypted = false,
  bool fAsSelf = false,
}) {
  assert(key.isNotEmpty);
  assert(value.isNotEmpty);
  assert(userName.isNotEmpty);
  // assert(type != null);
  // assert(persist != null);
  // assert(encrypted != null);
  // assert(!encrypted || (encrypted && fAsSelf != null));
  assert(!fAsSelf || (fAsSelf && encrypted));
  return _channel.invokeMethod<bool>('set', {
    'key': key,
    'value': value,
    'type': type.index + 1,
    'persist': persist.index + 1,
    'userName': userName,
    'encrypted': encrypted,
    'fAsSelf': fAsSelf,
  }).then((value) => value ?? false);
}