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

uninitialized model throws: Attempt to execute code removed by Dart AOT compiler (TFA) when compiled to exe #1415

Closed
KhaledBasiony opened this issue Oct 17, 2023 · 6 comments

Comments

@KhaledBasiony
Copy link

KhaledBasiony commented Oct 17, 2023

What happened?

I need to have an uninitialized model (let's call it Product) like this:

final Product prod;

if (something) {
    prod = Product('prod 1');
} else {
    prod = Product('prod 2');
}

the problem appears when I compile the app using dart compile exe ... and run it, it throws that error:
Attempt to execute code removed by Dart AOT compiler (TFA)

Notes:

  • It DOESN'T work whenever I specify the type Product
  • It DOES work if initialize it immediately (final prod = Product('prod');)
  • It DOES work if I don't use a type (final prod; for example)

I think I can work around it for now, I am just hoping this would get fixed 😀

if this not the appropriate place for this issue please guide me

Repro steps

  1. dart create dummy_project
  2. dart pub add realm_dart
  3. dart run realm_dart install
  4. create a file lib/model.dart with the following contents:
import 'package:realm_dart/realm.dart';

    part 'model.g.dart';
    
    @RealmModel()
    class _Product {}
    ```1. `dart run realm_dart generate`
1. put the following code in `bin/dummy_project.dart`:
```dart
import 'package:dummy_project/model.dart';

    void main(List<String> arguments) {
      final Product prod;
      prod = Product();
      print(prod);
    }
    ```1. `dart compile exe bin/dummy_project.dart -o bin/out`
1. `./bin/out`
1. panic :/
```sh
Unhandled exception:
Attempt to execute code removed by Dart AOT compiler (TFA)
#0      main (file:///home/khaled/Documents/dummy_project/bin/dummy_project.dart)
#1      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:294)
#2      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189)

Version

Dart SDK version: 3.1.0 (stable) (Tue Aug 15 21:33:36 2023 +0000) on "linux_x64"

What Atlas Services are you using?

Local Database only

What type of application is this?

Dart standalone application

Client OS and version

OS: openSUSE Tumbleweed x86_64

Code snippets

final Product prod;

if (something) {
    prod = Product('prod 1');
} else {
    prod = Product('prod 2');
}

Stacktrace of the exception/crash you're getting

Unhandled exception:
Attempt to execute code removed by Dart AOT compiler (TFA)
#0      main (file:///home/khaled/Documents/dummy_project/bin/dummy_project.dart)
#1      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:294)
#2      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189)

Relevant log output

No response

@milindgoel15
Copy link

Related to dart-lang/sdk#52874

Dont make the product final. Make it nullable

final Product prod; to Product? prod;

@KhaledBasiony
Copy link
Author

Hello @milindgoel15, thanks for the comment.

I already worked around it by using "late"

But really, I don't want to make it nullable when it's not, and I don't want to use "late" and lose the static analysis/lints, and obviously I don't want it to be dynamic.

I just thought it would be nice for this particular case to be solved.

Thanks anyway.

@milindgoel15
Copy link

You could do ternary check instead of assigning them in the if else.

final Product product = something ? First : second

@KhaledBasiony
Copy link
Author

Huh, clever!

Unfortunately my actual code is a bit more complicated than a simple boolean check.

Will try to make it work tho, thanks!

@dotjon0
Copy link

dotjon0 commented Oct 23, 2023

Huh, clever!

Unfortunately my actual code is a bit more complicated than a simple boolean check.

Will try to make it work tho, thanks!

If its a state, perhaps try https://riverpod.dev/, maybe a stream as then you can avoid nullable via an empty stream

@nielsenko
Copy link
Contributor

nielsenko commented May 6, 2024

There was a number of dart compiler bugs introduced with Dart 3 in relation to classes marked Finalizable (such as RealmObject subclasses). This is fixed by upgrading to 3.3 or later (sees #1648).

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 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