Skip to content

Fix DataClass.parseJson missing import prefix in modular mode#3775

Merged
simolus3 merged 1 commit intosimolus3:developfrom
hhDream:fix/modular-fromJsonString-prefix
Apr 3, 2026
Merged

Fix DataClass.parseJson missing import prefix in modular mode#3775
simolus3 merged 1 commit intosimolus3:developfrom
hhDream:fix/modular-fromJsonString-prefix

Conversation

@hhDream
Copy link
Copy Markdown

@hhDream hhDream commented Apr 3, 2026

Problem

When using write_from_json_string_constructor: true with drift_dev:modular mode, the generated fromJsonString constructor contains a bare DataClass.parseJson(...) reference without the import alias prefix (e.g. i0.), causing a compile error.

This is because data_class_writer.dart hard-codes the string DataClass instead of using _emitter.drift('DataClass') like the rest of the file does.

Fix

Use _emitter.drift('DataClass') to generate the correctly prefixed reference. This produces i0.DataClass.parseJson(...) in modular mode and DataClass.parseJson(...) in non-modular mode - both correct.

Reproduction

build.yaml:

drift_dev:modular:
  enabled: true
  options:
    write_from_json_string_constructor: true

Before fix (generated code, will not compile):

factory XxxData.fromJsonString(String encodedJson, ...) =>
    XxxData.fromJson(DataClass.parseJson(encodedJson) as Map<String, dynamic>, ...);
//                   ^^^^^^^^^ unresolved - drift is imported as i0

After fix:

factory XxxData.fromJsonString(String encodedJson, ...) =>
    XxxData.fromJson(i0.DataClass.parseJson(encodedJson) as Map<String, dynamic>, ...);

Closes #3774

Copy link
Copy Markdown
Owner

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@simolus3 simolus3 merged commit 3eb0786 into simolus3:develop Apr 3, 2026
11 checks passed
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: write_from_json_string_constructor + modular mode generates DataClass.parseJson(...) without i0. prefix, causing compile error.

3 participants