Skip to content

[Bug]: CsvPreviewer crashes with RangeError when CSV data is empty #1647

@Fikri-20

Description

@Fikri-20

Describe the bug/problem

The CsvPreviewer widget in lib/widgets/previewer_csv.dart attempts to access csvData[0] without checking if the CSV data list is empty. This causes a RangeError crash when the CSV response is empty or malformed.

Steps to Reproduce the bug/problem

  1. Send an API request that returns an empty CSV response
  2. Navigate to the Preview tab for CSV viewing
  3. Result: App crashes with RangeError: Index out of range

Alternatively:

  1. Send an API request with malformed CSV data that parses to an empty list
  2. View the response in Preview mode
  3. Result: App crashes

Expected behavior

The CSV previewer should gracefully handle empty CSV data by displaying an error widget instead of crashing. Empty CSV responses should not cause the application to fail.

Root Cause

File: lib/widgets/previewer_csv.dart, line 23

Current code:

final List<List<dynamic>> csvData = csv.decode(body);
return SingleChildScrollView(
  scrollDirection: Axis.vertical,
  child: SingleChildScrollView(
    scrollDirection: Axis.horizontal,
    child: DataTable(
      columns: csvData[0]  // ← Crashes if csvData is empty
          .map((item) => DataColumn(label: Text(item.toString())))
          .toList(),

Problem: The code directly accesses csvData[0] without verifying that the list contains at least one row.

Proposed Solution

Add a validation check before building the DataTable:

final List<List<dynamic>> csvData = csv.decode(body);

if (csvData.isEmpty) {
  return errorWidget;
}

return SingleChildScrollView(
  // ... rest of the code

Device Info

  • OS: All platforms (cross-platform issue)
  • Version: Not platform-specific
  • Environment: Production & Development

Impact

Severity: Medium - Causes application crash
Frequency: Occurs when API returns empty or malformed CSV data
User Impact: Prevents users from viewing other response formats after CSV crash, requires app restart

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions