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

An error FormatterException occurred while formatting the generated source #1739

Closed
shashankmathur8 opened this issue Jul 2, 2024 · 12 comments

Comments

@shashankmathur8
Copy link

What happened?

while running generate command for realm models in version 3.1.0. The part file is generated but got this error

[SEVERE] realm:realm_generator on lib/app/data/model/mongo_schema_model/app_logger.dart:
An error FormatterException occurred while formatting the generated source for
package:slb_gt_mobile/app/data/model/mongo_schema_model/app_logger.dart
which was output to
lib/app/data/model/mongo_schema_model/app_logger.realm.dart.
This may indicate an issue in the generator, the input source code, or in the
source formatter.
Could not format because the source could not be parsed:

line 90, column 8 of .: Expected an identifier.

90 │ return switch (ejson) {
│ ^^^^^^

line 90, column 1 of .: Expected to find ';'.

90 │ return switch (ejson) {
│ ^^^^^^

line 91, column 1 of .: Expected to find 'case'.

91 │ {
│ ^

Repro steps

  1. Create realm model file
  2. Run Generate command
  3. Get Error.

Version

3.22.0,

What Atlas Services are you using?

Atlas Device Sync

What type of application is this?

Flutter Application

Client OS and version

13.6

Code snippets

import 'package:realm/realm.dart';
part 'app_logger.realm.dart';

// NOTE: These Realm models are private and therefore should be copied into the same .dart file.
@RealmModel()
class _AppLogger {
@PrimaryKey()
@mapto('_id')
ObjectId? id;
String? alias;
String? date;
String? message;
String? origin;
String? type;
String? userTruckMappingId;
}

Stacktrace of the exception/crash you're getting

[SEVERE] realm:realm_generator on lib/app/data/model/mongo_schema_model/app_logger.dart:
An error `FormatterException` occurred while formatting the generated source for
  `package:slb_gt_mobile/app/data/model/mongo_schema_model/app_logger.dart`
which was output to
  `lib/app/data/model/mongo_schema_model/app_logger.realm.dart`.
This may indicate an issue in the generator, the input source code, or in the
source formatter.
Could not format because the source could not be parsed:

line 90, column 8 of .: Expected an identifier.
   ╷
90 │ return switch (ejson) {
   │        ^^^^^^
   ╵
line 90, column 1 of .: Expected to find ';'.
   ╷
90 │ return switch (ejson) {
   │ ^^^^^^
   ╵
line 91, column 1 of .: Expected to find 'case'.
   ╷
91 │ {
   │ ^
   ╵

Relevant log output

No response

Copy link

sync-by-unito bot commented Jul 2, 2024

➤ PM Bot commented:

Jira ticket: RDART-1065

@shashankmathur8
Copy link
Author

Error (Xcode): lib/app/modules/inventory/models/consignmentSchema.realm.dart:330:8: Error: Expected an identifier, but got 'switch'.

Could not build the application for the simulator.
Error launching application on iPad (10th generation).

@nirinchev
Copy link
Member

Switch expressions should be supported - can you make sure you've updated to the current versions of dart and flutter?

@shashankmathur8
Copy link
Author

Switch expressions should be supported - can you make sure you've updated to the current versions of dart and flutter?

Yes, Current version is 3.22.0 , I've checked and update to 3.22.2 which is latest stable version. Still facing this issue.

@nielsenko
Copy link
Contributor

See https://dart.dev/language/branches#switch-expressions

So flutter is 3.22.2, but what does dart --version say, when you run it on the command line?

@shashankmathur8
Copy link
Author

dart --version

Dart SDK version: 3.4.3 (stable) (Tue Jun 4 19:51:39 2024 +0000) on "macos_arm64"

@nielsenko
Copy link
Contributor

What is the min SDK version in your pubspec.yaml?

@shashankmathur8
Copy link
Author

What is the min SDK version in your pubspec.yaml?

environment:
sdk: '>=3.3.1 <4.0.0'

@nielsenko
Copy link
Contributor

Can you you upload your pubspec.lock file?

@cmendes0101
Copy link

I had the same issue today with introducing Realm to my existing project. Same latest Flutter and Dart under Windows. I had tried downgrading but it continued to error.

I cleared the pubspec.lock file and had it regenerate, after that the command dart run realm generate worked fine.

@shashankmathur8
Copy link
Author

Found the issue, Switch expressions require a language version of at least 3.0, In my pubspec.yaml the environment sdk version was sdk: '>=2.19.0 <3.0.0' , because of which the switch expression was throwing the error. Changing it to sdk: '>=3.0.0 <=3.4.3' solved my issue.
Flutter Clean
and then Flutter pub get
Done.

@nielsenko
Copy link
Contributor

nielsenko commented Jul 5, 2024

@shashankmathur8 I was confused by you earlier comment that indicated you already had valid sdk constraints.

Realm has required Dart 3.0, since version 1.3, which was released roughly a year ago (see https://pub.dev/packages/realm/versions). Latest version actually requires Dart 3.3, a requirement that was introduced with realm version 2.2.0.

Also, I would recommend loosing the upper bound a bit. 3.4.3 is the current stable version of Dart. Something like:

environment:
  sdk: '>=3.3.0 <4.0.0'

@nielsenko nielsenko self-assigned this Jul 5, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants