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

Not able to create `Realm` object with different `schemaObjects` which uses `writeAsync()` method

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      What happened?

      I encounter an issue when attempting to instantiate a Realm object with different schemaObjects while utilizing the writeAsync() method. I've created two distinct functions to write data, each internally invoking realm.writeAsync(). Both functions initialize a Realm object by invoking its constructor with a specific configuration. Notably, each function has a different schemaObject specified in its configuration.

      When these two functions are called within my code, the debugger becomes unresponsive during the initialization step of the Realm object within these functions. No exceptions are thrown, and the issue remains unresolved. However, if I precede these function calls with await, the code behaves as expected. Additionally, replacing realm.writeAsync() with realm.write() results in expected behavior.

      Note: on modifying the code as described (replacing writeAsync() with write() or preceding function calls with await), it is necessary to clear the application data and uninstall the application to reproduce this issue.

      Repro steps

      1. Create 2 different RealmModel
      2. Create 2 different function to utilize writeAsync() method
      3. call these 2 function without putting await in main code

      NOTE: i have added full code snippet to reproduce this issue

      Version

      Flutter 3.19.3 Dart 3.3.1

      What Atlas Services are you using?

      Local Database only

      What type of application is this?

      Flutter Application

      Client OS and version

      Android SDK version - 33

      Code snippets

      //car.dart

      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
      import 'package:realm/realm.dart';
      
      part 'car.realm.dart'; 
      
      @RealmModel()
      class _Car {
        late String make;
      
        late String model;
      
        int? kilometers = 500;
      }
      }
      

      //hotel.dart

      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
      import 'package:realm/realm.dart';  
      
      part 'hotel.realm.dart'; 
      
      @RealmModel() 
      class _Hotel {
      
        late String city;
        late int price;
      
      }
      

      //main.dart

      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
      import 'package:flutter/material.dart';
      import 'package:flutter_application_1/car.dart';
      import 'package:flutter_application_1/hotel.dart';
      import 'package:realm/realm.dart';
      
      void main() {
        runApp(const MainApp());
      }
      
      class MainApp extends StatelessWidget {
        const MainApp({super.key});
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            home: Scaffold(
              body: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    ElevatedButton(
                      onPressed: () {
                        debugPrint("Hello World");
                      },
                      child: const Text("Click me"),
                    ),
                    ElevatedButton(
                        onPressed: () {
                          // add await here to get expected result
                          writeCarData().then((value) => debugPrint("Car data written"));
                          writeHotelData().then((value) => debugPrint("Hotel data written"));
                        },
                        child: const Text("Click me")),
                  ],
                ),
              ),
            ),
          );
        }
      }
      
      Future<void> writeCarData() async {
        var config = Configuration.local([Car.schema]);
        var realm = Realm(config);
      
        var car = Car("Tesla", "Model Y", kilometers: 5);
        await realm.writeAsync(() {
          realm.add(car);
        });
      }
      
      Future<void> writeHotelData() async {
        var config = Configuration.local([Hotel.schema]);
        var realm = Realm(config);
      
        var hotel = Hotel(
          "abc",
          1000,
        );
        await realm.writeAsync(() {
          realm.add(hotel);
        });
      }
      

      Stacktrace of the exception/crash you're getting

      No response

      Relevant log output

      No response

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: