Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 667 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flutter JSON Serialization - Not generating *.g.dart files

#1
I am new to flutter and the objective is to serialise complex JSON objects which contain other smaller objects.

Using the `json_serializable: ^2.0.0` and the `pubspec.yaml` file looks something like this.


dependencies:
intl: ^0.15.7
json_annotation: ^2.0.0
built_value: ^6.7.1
flutter:
sdk: flutter

dev_dependencies:
build_runner: ^1.0.0
json_serializable: ^2.0.0
built_value_generator: ^6.7.1
flutter_test:
sdk: flutter

The `user.dart` look like this

import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable(nullable: false)
class User {
final String firstName;
final String lastName;
final DateTime dateOfBirth;
User({this.firstName, this.lastName, this.dateOfBirth});
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}

I have tried `flutter pub run build_runner build` yet the file user.g.dart doesn't get created and I am facing issue with that.

I also have added the `build.yaml` file with following code

targets:
$default:
builders:
built_value_generator|built_value:
generate_for:
- model/*.dart
json_serializable|json_serializable:
generate_for:
- model/*.dart

Can anyone let me know what I am missing here. Thanks
Reply

#2
The constructor's argument shouldn't be optional

User({this.firstName, this.lastName, this.dateOfBirth});

They should be obligatory:

User(this.firstName, this.lastName, this.dateOfBirth);

And the part

'user.g.dart';

should be matching the Uppercase User class:

part 'User.g.dart';
Reply

#3
The file name, class, and `part 'Book.g.dart';` should all match.
Reply

#4
I had this following error

error

[WARNING] json_serializable:json_serializable on lib/day10/models/sentence.dart:
Missing "part 'sentence.g.dart';".

I noticed that I had to change on the model file this `part 'Sentence.g.dart';` to this `part 'sentence.g.dart';` in other words, I had to lowercase it.
Reply

#5
try running

>flutter packages pub run build_runner build --delete-conflicting-outputs
Reply

#6
I got to know that , file has to be saved before running the command
`flutter packages pub run build_runner build`

I know answer seems so simple.
Reply

#7
in my case I had to add **hive_generator** package, along with the build_runner package.
If your model class name is for example transaction.dart, then it would be like this-
``` part 'transaction.g.dart' ```
Run the command-
```flutter packages pub run build_runner build``` it will generate the transaction model for you.
Reply

#8
adding

json_serializable: ^6.0.0

to dev_dependencies solve it for me
Reply

#9
If you are trying to generate .g files by just running the command `flutter packages pub run build_runner build` and you are not getting your desired result then just try:

1. `flutter clean`

2. 'flutter pub cache repair'

3. `flutter packages pub run build_runner build --delete-conflicting-outputs`

4. delete all the gradle temp files

5. then run `flutter pub get`

6. `flutter packages pub run build_runner build`.


Reply

#10
Some times we forgot to save the files, please save the file then run the command.

```sh
flutter packages pub run build_runner build
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through