I have a private list _barbershops, i used it inside the methode getBarbershopCollectionFromFirebase(), i added to it some elements from firestore when i print its value in the end of the method i can see elements that are instences of that list's type... meaning it is full and firestore did its part with no error. However trying to print it outside of the methode (with its getter) it returns an empty list
here is my code file:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hafefly/models/babershopModel.dart';
class BarbershopService {
FirebaseFirestore? _instance;
List<Barbershop> _barbershops = [];
List<Barbershop> getBarbershops() {
print(_barbershops);
return _barbershops;
}
Future<void> getBarbershopCollectionFromFirebase() async {
_instance = FirebaseFirestore.instance;
CollectionReference barbershopsCollection =
_instance!.collection('barbershops');
QuerySnapshot barbershops = await barbershopsCollection.get();
for (var element in barbershops.docs) {
DocumentSnapshot snapshot =
await barbershopsCollection.doc(element.id).get();
var data = snapshot.data() as Map<String, dynamic>;
Barbershop barb = Barbershop(
name: data['name'],
rating: data['rating'],
vip: data['vip'],
location: data['location'],
latitude: data['latitude'],
longitude: data['longitude'],
open: data['open'],
close: data['close']);
_barbershops.add(barb);
print(_barbershops);
}
}
}
Console log for the print inside the method:
I/flutter ( 6864): [Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop', Instance of 'Barbershop']
Console log for the print inside the getter:
I/flutter ( 6864): []