1

This is My Repository

class DB {
  final db = FirebaseFirestore.instance;

  Stream<QuerySnapshot> init(UserModel user) {
    return db
        .collection('CollectionName')
        .doc(user.email) //this is a unique value which i want to retrieve the value from main after successful login
        .collection('New Collection')
        .snapshots();
  }


  void readData(String id, UserModel user) async {
    DocumentSnapshot snapshot = await db
        .collection('Collection Name')
        .doc(user.email)
        .collection('New Collection')
        .doc(id)
        .get();
    // ignore: avoid_print
    print(snapshot['name']);
  }


}

DB db = DB();

This is My BlocFile

class IncidentBloc implements BlocBase {
  IncidentBloc(UserModel user) {
    db.init(user).listen((data) => _inFirestore.add(data));
  }

  final _idController = BehaviorSubject<String>();
  Stream<String> get outId => _idController.stream;
  Sink<String> get _inId => _idController.sink;

  final _firestoreController = BehaviorSubject<QuerySnapshot>();
  Stream<QuerySnapshot> get outFirestore => _firestoreController.stream;
  Sink<QuerySnapshot> get _inFirestore => _firestoreController.sink;

  void readData(UserModel user) async {
    db.readData(id, user);
  }


  @override
  void dispose() {
    _firestoreController.close();
    _idController.close();
  }
}

And This is my main

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  BlocOverrides.runZoned(
    () => runApp(
      BlocProviderr(bloc: IncidentBloc(UserModel()), child: const App())

    ),
    blocObserver: AppBlocObserver(),
  );
}

It seems that the UserModel is null or empty how do i pass value to my IncidentBloc? And this is after a successful login/authentication. If I do it like this in main: "IncidentBloc(UserModel(email: 'abcde.t@gmail.com'))" It is working, but i want it to dynamically retrieve data based on the user's email not the hardcoded 'abcde.t@gmail.com'

discreet
  • 35
  • 7

1 Answers1

1

Based on your code, you will need to get the user's email from Firebase and pass it into Incident Bloc. This StackOverflow answer explains how to do that; so does this one.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  <FirebaseUser> user = await _auth.currentUser();
  final mailID = user.email;
  BlocOverrides.runZoned(
    () => runApp(
      BlocProviderr(bloc: IncidentBloc(UserModel(email: mailID)), child: const App())

    ),
    blocObserver: AppBlocObserver(),
  );
}

TarHalda
  • 1,050
  • 1
  • 9
  • 27