Thapelo Radebe
Thapelo Radebe

Reputation: 627

Why am I getting the Stack Overflow error?

I get a Stack Overflow error but I don't seem to know what's wrong. This is Database class helps me to get FirebaseDatabase information.

 import 'package:firebase_database/firebase_database.dart';
    import 'User.dart';
    import 'dart:async';


    class FireDatabase{
      FireDatabase();

      DatabaseReference userRef = FirebaseDatabase.instance.reference().child("users");
      DatabaseReference transactionsRef = FirebaseDatabase.instance.reference().child("transactions");
      User user = new User();


     getDatabaseUser(String cUid){
        userRef.orderByChild(cUid).once().then((DataSnapshot data){
          if (data.value!=null) {
            return data.value;
          } else {
            print("prints - database.dart : data.value of user is empty/null");
            return null;
          }
        },onError: (e){
          print("prints - database.dart " + e.toString());
          return null;
        });
        return null;
      }

      getDatabaseTransactions(String cUid) {
        transactionsRef.orderByChild("transactions").once().then((DataSnapshot data){
          if(data.value!=null) {
            return data.value;
          } else {
            print("prints - database.dart : data.value of user is empty/null");
            return null;
          }
        },onError: (e){
          print("prints - database.dart " + e.toString());
          return null;
        });
        return null;
      }

      getDatabaseTransaction(String cUid) {
        transactionsRef.orderByChild(cUid).limitToFirst(1).once().then((DataSnapshot data){
          if(data.value!=null) {
            return data.value;
          } else {
            print("prints - database.dart : data.value of user is empty/null");
            return null;
          }
        },onError: (e){
          print("prints - database.dart " + e.toString());
          return null;
        });
        return null;
      }

    }

Upvotes: 8

Views: 20846

Answers (4)

Jason
Jason

Reputation: 933

Try using a static variable when instantiating. Static variables get initialized once and act like Singletons.

class Point {
    int? x;
    int? y;
    Point({this.x, this.y});
    static final Point instance = Point();
}

Upvotes: 0

Aleem Ehsan
Aleem Ehsan

Reputation: 11

for me in my class of FireBaseServices i was creating an instance of itself which was causing the error.

 class FireBaseServices {
   FireBaseServices _fireBaseServices_instance = FireBaseServices();

stackOverflow was occuring due to some sort of Recursion happening.

I fixed it by removing the instance creation line.

Yeppeeeee.... it worked well. Thanks Flutter Community.

Upvotes: 0

MendelG
MendelG

Reputation: 20098

For me, the problem was that I had 2 classes, now within both classes, I instantiated each other class in a recursive manner.

Here is an example:

class Firestore{
  final auth = Auth();
}


class Auth{
  final fireStore = Firestore();
}

As you can see each class is calling the other class, that what caused the StackOverflow.

Upvotes: 6

Thapelo Radebe
Thapelo Radebe

Reputation: 627

Instantiating a User class in the Database class

User user = new User();

seem to have been the problem but luckily I was not using it anyway, the User gets stored in the User class

Upvotes: 0

Related Questions