Dart / Editorial solution guide

Invalid text becomes a zero or failed quantity

Trace parsing and domain validation before a nullable value enters a calculation.

About 2 min readContent reviewed

Read-only knowledge base · No public comments

Recognize the symptoms

A blank or malformed quantity causes an exception, becomes zero, or produces a misleading total. Ordinary numeric input may appear to work, making the failure easy to miss. Preserve a synthetic input and the parsed value before changing the calculation.

Understand the likely causes

  • Parsing failure can return null, while a successfully parsed integer may still violate the allowed range. A forced null assertion or fallback to zero can hide the distinction between rejected input and a valid free price.

Reproduce the input boundary

Test empty text, whitespace, "3", "0", a negative number, a fraction, and nonnumeric text. Record whether parsing succeeds and whether the domain accepts the result. Do not log real customer input. Determine whether the field is required and define its allowed range before selecting a fallback; the correct policy depends on the application.

Parse and branch explicitly

The example trims the input, uses int.tryParse, and rejects nonpositive values. The caller checks null before multiplying, so a rejected quantity cannot become a successful zero total. This sample uses null for all invalid cases; an application requiring field-specific feedback can return a more descriptive result. Add an upper bound and validate price independently where the domain requires them.

dart Reject a nonpositive quantity
int? parseQuantity(String text) {
  final value = int.tryParse(text.trim());
  if (value == null || value < 1) return null;
  return value;
}

void main() {
  final quantity = parseQuantity("0");
  if (quantity == null) {
    print("invalid quantity");
  } else {
    print(quantity * 250);
  }
}

The parser returns null for zero and the caller branches before calculating.

Keep trust boundaries separate

Client-side validation improves feedback but does not make a server request trustworthy. Validate again at the receiving boundary and ensure malformed data cannot trigger an order or other effect. Preserve the original text for correction without exposing it in logs. Do not replace a null assertion with a blanket default unless that default is an explicitly valid domain decision.

Verify valid and rejected paths

Run the same input matrix after the repair and check that invalid cases take the documented error branch. Check a positive quantity with zero unit price separately if free items are valid, so it is not confused with invalid input. Test the product’s upper bound and any platform arithmetic limits. Verify that no rejected input causes a downstream mutation, and retain the fixture as a regression test.

Verify the fix

Confirm that "3" is accepted and empty, malformed, zero, negative, and fractional inputs are rejected under this contract. Test a valid zero price separately.

Keep it from recurring

  • Document representation and domain rules, avoid force-unwrapping external input, and test boundaries at both client and server.

Official references