toBuffer function

Uint8List toBuffer(
  1. dynamic v
)

Attempts to turn a value into a Uint8List. As input it supports Uint8List, String, int, null, BigInt method.

Implementation

Uint8List toBuffer(v) {
  if (!(v is Uint8List)) {
    if (v is List) {
      v = Uint8List.fromList(v);
    } else if (v is String) {
      if (isHexString(v)) {
        v = Uint8List.fromList(hex.decode(padToEven(stripHexPrefix(v))));
      } else {
        v = Uint8List.fromList(utf8.encode(v));
      }
    } else if (v is int) {
      v = intToBuffer(v);
    } else if (v == null) {
      v = Uint8List(0);
    } else if (v is BigInt) {
      v = Uint8List.fromList(encodeBigInt(v));
    } else {
      throw 'invalid type';
    }
  }

  return v;
}