0

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): []

DziKo
  • 13
  • 3
  • 1
    Your response is an array, not a map. loop in 'data' – Benjith Kizhisseri Apr 05 '22 at 04:50
  • Are you sure that you're calling `getBarbershops()` *after* `getBarbershopCollectionFromFirebase()` *completes*? You don't show where you're calling each function. Most likely you're calling `getBarbershopCollectionFromFirebase()` and not waiting for it to complete before calling `getBarbershops()`. – jamesdlin Apr 05 '22 at 05:19
  • I am calling them in home.dart to use the list as a data for a widget, ` BarbershopService barbsp = Provider.of(context, listen: false); BarbershopService().getBarbershopCollectionFromFirebase(); List barbershops = BarbershopService().getBarbershops(); ` I also tried calling getBarbershopCollectionFromFirebase() in getBarbershops() as follows: ` List getBarbershops() { getBarbershopCollectionFromFirebase(); print(_barbershops); return _barbershops; } ` but no luck @jamesdlin – DziKo Apr 05 '22 at 10:28
  • can you explain more please ? @BenjithKizhisseri – DziKo Apr 05 '22 at 10:43
  • You're calling `BarbershopService().getBarbershopCollectionFromFirebase(); List barbershops = BarbershopService().getBarbershops();`. The `getBarbershopCollectionFromFirebase()` method returns a `Future`; it is asynchronous, and you must wait for it to complete first if you expect `getBarbershops()` to return the filled `List`. Do: `await BarbershopService().getBarbershopCollectionFromFirebase(); List barbershops = BarbershopService().getBarbershops();`. Also see [What is a Future, and how do I use it?](https://stackoverflow.com/q/63017280/) – jamesdlin Apr 05 '22 at 15:00

1 Answers1

0

This is how I solved it:

  1. I called getBarbershopFromFirebase() inside the getBarbershops() method

  2. in the home.dart (where I wanted to use the widget based on this data) I fetched the data using this method:

    _fetchListItems() async {
     List<Barbershop>? barbershopsList =
         await BarbershopService().getBarbershops();
    
     setState(() {
       for (var item in barbershopsList) {
         _barbershops.add(item);
       }
     });
    
     return _barbershops;
    }
    
  3. I called the _fetchListItems() inside the initState()

  4. I initialised a Future<List<Barbershops>> barbershops

  5. I used a FutureBuilder() with a future: barbershops

  6. with the help of ListView.builder I put itemCount: _barbershops.length

  7. in the itemBuilder(context, index) I replaced the hard coded data with _barbershops[index]

I hope I helped someone with my journey experience.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
DziKo
  • 13
  • 3