realm / realm-dart

Realm is a mobile database: a replacement for SQLite & ORMs.
Apache License 2.0
772 stars 86 forks source link

An error `FormatterException` occurred while formatting the generated source #1739

Closed shashankmathur8 closed 4 months ago

shashankmathur8 commented 4 months ago

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

sync-by-unito[bot] commented 4 months ago

➤ PM Bot commented:

Jira ticket: RDART-1065

shashankmathur8 commented 4 months ago

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 commented 4 months ago

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

shashankmathur8 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

dart --version

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

nielsenko commented 4 months ago

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

shashankmathur8 commented 4 months ago

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

environment: sdk: '>=3.3.1 <4.0.0'

nielsenko commented 4 months ago

Can you you upload your pubspec.lock file?

cmendes0101 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

@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'