declare method

void declare(
  1. String name,
  2. Object? value, {
  3. bool isConst = false,
  4. bool isFinal = true,
  5. String? type,
})

Declares a variable.

Implementation

void declare(
  String name,
  Object? value, {
  bool isConst = false,
  bool isFinal = true,
  String? type,
}) {
  final list = <String>[];
  if (isConst) {
    list.add('const');
    if (value is String) {
      value = value.trimLeft();
      if (value.startsWith('const')) {
        value = value.substring(5);
        value = value.trimLeft();
      }
    }
  } else if (isFinal) {
    list.add('final');
  }

  final isVar = !(isConst || isFinal || type != null);
  if (isVar) {
    list.add('var');
  }

  if (type != null && !isVar) {
    list.add(type);
  }

  list.add(name);
  if (value != null) {
    list.addAll(['=', value.toString()]);
  }

  final code = list.join(' ');
  stmt(code);
}