0

I made my first app with flutter, it uses a complex data structure with lists of objects nested inside of lists of objects. Now that the app is pretty much finished in terms of functionality, I'm trying to save the app's data using firebase/firestore.

First time working with JSON, so I'm finding it a little confusing but essentially when I tried to write a top level object containing all the other nested lists of objects etc, I get an error along the lines of 'returns instance of Class' instead of actually saving the specific object.

After some research, it seems I need to use something called JSON Serialization to handle this. I added the JSON serialization, annotation and build runner dependencies to pubspec.yaml, and in each of the model definitions I added:

'part 'class.g.dart''

@JsonSerializable(anyMap: true, explicitToJson: true)

factory Class.fromJson(Map<String, dynamic> data) => _$ClassFromJson(data);

  Map<String, dynamic> toJson() => _$ClassToJson(this);

now, when I try flutter pub run build_runner build, I get the error:

Could not generate fromJson code for mesocycles because of type Mesocycle. package:nav_dummy/models/block.dart:7:19 ╷ 7 │ List mesocycles = []; │ ^^^^^^^^^^ ╵ [SEVERE] Failed after 190ms pub finished with exit code

For some context, the List mesocycles = []; is inside the second layer, i.e. Class1.Class2.mesocycles and it contains the 3rd level objects of type Mesocycle.

Not sure why I'm getting this error when the first level class also contains a list of objects, and it doesn't seem to be a problem according to terminal error report.

Please let me know if you have any idea how I may proceed. The app is ready now, getting it to play nicely with the database is holding me back and extremely frustrating.

UPDATE: I looked at this post

Flutter JSON Serialization - Not generating *.g.dart files

and found my error described under 'Could not generate fromJsoncode forsomeField' - yet my nested class IS annotated with @JsonSerializable(explicitToJson: true) and I'm still getting the same error from build_runner and am unable to generate the *.g.dart files... really not sure why this is happening, unless it has something to do with the fact that the nested class is stored in a list, however as I stated above, the top level object is structured the exact same way, with a nested list of object of another class, and it doesn't seem to be throwing any errors...

Ken White
  • 123,280
  • 14
  • 225
  • 444

1 Answers1

0

it finally generated the .g.dart class files after saving all and restarting vscode. since the previous build happned before i added the new code to all of the models, when i ran build_runner, those changes weren't being recognized.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 14 '22 at 02:39