Uploaded image for project: 'Realm Dart SDK'
  1. Realm Dart SDK
  2. RDART-992

Realm generator rejects const values saying initialization must be const

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      What happened?

      Using constant classes or negative numbers used to work for initialization. When upgrading from 1.6 to 2.0, this no longer works:

      Field initializers must be constant
      
      in: package:sustain/model/data/picture.dart:19:14
          ╷
      16  │ @RealmModel()
      17  │ class _Example {
          │       ━━━━━━━━ in realm model for 'Example'
      ... │
      19  │   double y = -1;
          │              ^^ Must be const
          ╵
      Ensure the default value for field "y" is const
      
      

      Repro steps

      Create a class with integer or float field and try to initialize it to a constant negative value.
      Presumably this would work (or rather fail to work) with any other class that provides a const constructor.

      Version

      2.0.0

      What Atlas Services are you using?

      Local Database only

      What type of application is this?

      Flutter Application

      Client OS and version

      Linux

      Code snippets

      Unable to find source-code formatter for language: dart. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      @RealmModel()
      class _Example {
        double x = 0;
        double y = -1;
        int z = -1;
      }
      

      Stacktrace of the exception/crash you're getting

      No response

      Relevant log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      [SEVERE] realm:realm_generator on lib/model/data/picture.dart:
      
      Field initializers must be constant
      
      in: package:sustain/model/data/picture.dart:19:14
          ╷
      16  │ @RealmModel()
      17  │ class _Example {
          │       ━━━━━━━━ in realm model for 'Example'
      ... │
      19  │   double y = -1;
          │              ^^ Must be const
          ╵
      Ensure the default value for field "y" is const
      
      
      

            Assignee:
            kasper.nielsen@mongodb.com Kasper Nielsen
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: