Skip to content

fix: guard null content in saveToDownloads to prevent null-check crash (#1704)#1708

Open
deepak0x wants to merge 1 commit into
foss42:mainfrom
deepak0x:fix/save-utils-null-content
Open

fix: guard null content in saveToDownloads to prevent null-check crash (#1704)#1708
deepak0x wants to merge 1 commit into
foss42:mainfrom
deepak0x:fix/save-utils-null-content

Conversation

@deepak0x

Copy link
Copy Markdown
Contributor

PR Description

saveToDownloads() in lib/utils/save_utils.dart accepted a nullable Uint8List? content but force-unwrapped it with content! inside the if (path != null) block. When a save path is resolved (user picks a location) but content is null (e.g. an empty or failed response body), this threw Null check operator used on a null value.

This PR removes the content! force-unwrap and adds an explicit null guard: when content is null, the user now sees a "No content available to save." snackbar instead of an unhandled crash. Behaviour for valid content and for an unresolved path is unchanged.

Related Issues

Checklist

  • I have gone through the contributing guide
  • I have updated my branch and synced it with project main branch before making this PR
  • I am using the latest Flutter stable branch (run flutter upgrade and verify)
  • I have run the tests (flutter test) and all tests are passing

Added/updated tests?

  • Yes

Added a regression test in test/utils/save_utils_test.dart ("saveToDownloads handles null content without crashing") that calls saveToDownloads with content: null and asserts a snackbar is shown rather than an exception being thrown.

OS on which you have developed and tested the feature?

  • Windows
  • macOS
  • Linux

Replace the force-unwrap content! with an explicit null guard so a
null content (e.g. empty/failed response body) shows a friendly
snackbar instead of throwing 'Null check operator used on a null
value'. Add a regression test covering null content.

Closes foss42#1704
@deepak0x

Copy link
Copy Markdown
Contributor Author

@animator raised the fix as requested in #1704. Minimal change + regression test. Ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Null crash when saving response with null content

1 participant