Search code examples
databasemongodbflutter

Flutter: Realmdb "Getter Schema isn't defined for the type Transaction"


Am trying to connect my flutter app to realmdb using the sdk, I installed the packages needed, then created the model and generated the script using:

flutter pub run realm generate --watch

when I use this command final realm = Realm(Configuration.local([Transaction.schema])); in main.dart it tells me:

The getter 'schema' isn't defined for the type 'Transaction'.
Try importing the library that defines 'schema', correcting the name to the name of an existing getter, or defining a getter or field named 'schema'.dartundefined_getter

main.dart

import 'dart:io';
import './classes/client_transaction.dart' as transaction;
import 'package:flutter/material.dart';
import 'package:client/login.dart';
import 'package:client/register.dart';
import 'package:client/overview.dart';
import 'package:client/Home.dart';
import 'package:http/http.dart' as http;
import 'package:firebase_core/firebase_core.dart';
import 'package:client/classes/auth.dart';
import 'package:realm/realm.dart';
import 'firebase_options.dart';
import 'package:provider/provider.dart';

void main() async {
  final realm = Realm(Configuration.local([Transaction.schema]));

client_transaction.dart

import 'package:realm/realm.dart';

part 'client_transaction.g.dart';

@RealmModel()
class _Transaction {
  late String uid;
  late String name;
  late String category;
  late double amount;
  late String date;
}

the 'client_transaction.g.dart' file was generated successfully.

I tried changing the name of the file, and regenerating the scripts but it didn't work.


Solution

  • I ended up solving it by opening the realm inside the client_transaction.dart file and then calling the method from main.dart.

    client_transaction.dart:

    import 'package:realm/realm.dart';
    
    part 'client_transaction.g.dart';
    
    @RealmModel()
    class _Transaction {
      late String uid;
      late String name;
      late String category;
      late double amount;
      late String date;
    }
    
    init() {
      var config = Configuration.local([Transaction.schema]);
      var realm = Realm(config);
      var t = Transaction("123", "TestName", "food", 55.1, "13/13/2013");
      realm.write(() {
        realm.add(t);
      });
      var z= realm.all<Transaction>();
      Transaction x= z[0];
    }
    

    main.dart:

    import 'dart:io';
    import 'package:client/classes/client_transaction.dart' as transactions;
    import 'package:flutter/material.dart';
    import 'package:client/login.dart';
    import 'package:client/register.dart';
    import 'package:client/overview.dart';
    import 'package:client/Home.dart';
    // import 'package:http/http.dart' as http;
    import 'package:firebase_core/firebase_core.dart';
    import 'package:client/classes/auth.dart';
    import 'package:realm/realm.dart';
    import 'firebase_options.dart';
    import 'package:provider/provider.dart';
    
    void main() async {
      transactions.init();