Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros: Serialisation issue from different file #56646

Open
varunbhalerao56 opened this issue Sep 4, 2024 · 0 comments
Open

Macros: Serialisation issue from different file #56646

varunbhalerao56 opened this issue Sep 4, 2024 · 0 comments
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library

Comments

@varunbhalerao56
Copy link

Steps to reproduce

1. Create a Flutter project with macros enabled, in the Flutter and Dart versions mentioned below.

flutter & dart version

Flutter 3.25.0-1.0.pre.252 • channel master • https://github.com/flutter/flutter.git
Framework • revision ab143f147e (2 hours ago) • 2024-09-04 05:12:25 -0400
Engine • revision 68214414d3
Tools • Dart 3.6.0 (build 3.6.0-198.0.dev) • DevTools 2.39.0

2. Enable the macros.

pubspec_yaml

environment:
    sdk: ">=3.6.0-0 <4.0.0"

...

dependencies:
    json: ^0.20.2

analysis_options.yaml

include: package:flutter_lints/flutter.yaml

analyzer:
    errors:
        non_nullable_equals_parameter: ignore
    enable-experiment:
        - macros
        - enhanced-parts

enhanced-parts will be required now due to the following dart-lang/language#4072

3. Create two files model.dart and model_two.dart

(check code in code sample)

4. Run

After running to project and hot reloading it. The following error will appear.

lib/playground/model.dart:14:9: Error: Unable to deserialize type, it must be a native JSON type or a type with a `fromJson(Map<String, Object?> json)` constructor.
  final TestTwo testTwo;
        ^
lib/playground/model.dart:14:9: Error: Unable to serialize type, it must be a native JSON type or a type with a `Map<String, Object?> toJson()` method.
  final TestTwo testTwo;
        ^
Restarted application in 1,592ms.

Issue

But currently the model that is being imported from the model_two.dart file gives a the serialization error.

The same model when placed in the same file as model.dart does not give the error. The error only seems to occur on models that are being imported from outside files.

MainModel contains TestOne model which is located in the model.dart file and a TestTwo model which is located in the model_two.dart file. The error is only given on the TestTwo model.

Code Sample

Code sample

model.dart

import 'package:json/json.dart';
import 'package:np_combine/model_two.dart';

@JsonCodable()
class TestOne {
  String name;
  int age;
  TestOne({required this.name, required this.age});
}

@JsonCodable()
class MainModel {
  final TestOne testOne;
  final TestTwo testTwo;

  MainModel({required this.testOne, required this.testTwo});
}

model_two.dart

import 'package:json/json.dart';

@JsonCodable()
class TestTwo {
  String name;
  int age;
  TestTwo({required this.name, required this.age});
}
@varunbhalerao56 varunbhalerao56 changed the title Macros: Serialisation Issue Macros: Serialisation issue from different file Sep 4, 2024
@lrhn lrhn transferred this issue from dart-lang/language Sep 4, 2024
@lrhn lrhn added area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library labels Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library
Projects
Development

No branches or pull requests

2 participants