简体   繁体   中英

Flutter Firestore Query snapshot- result is always null

I have a simple flutter code to retrieve some data from Firestore. the data is retireved correctly, however passing the data from the future function making the result always null. can you advise how to adapt the code to return the list?

that is the class where the actual query is happening:

class DatabaseManager {
  final CollectionReference BusinessProfilesCollection =
      FirebaseFirestore.instance.collection("BusinessProfilesCollection");

  Future GetBusinessProfilesCollection() async {
    List businessprofileslist = [];
    try {
      await BusinessProfilesCollection.get().then((QuerySnapshot) {
        QuerySnapshot.docs.forEach((element) {
          businessprofileslist.add(element.data());
          print(businessprofileslist[0]);
        });
      });
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

here is the page where I am calling the function: (however the result is always null)

class _ProfilesListPageState extends State<ProfilesListPage> {
  List businessprofileslist = [];

  @override
  void initState() {
    super.initState();
    fetchBusinessProfilesList();
  }

  fetchBusinessProfilesList() async {
    dynamic result = await DatabaseManager().GetBusinessProfilesCollection();
    print(result.toString());
    if (result == null) {
      print('enable to retieve');
    } else {
      print('success');
      setState(() {
        businessprofileslist = result;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}

You're not returning anything from GetBusinessProfilesCollection but null , so the result seems somewhat expected.

I guess you want to do:

class DatabaseManager {
  final CollectionReference BusinessProfilesCollection =
      FirebaseFirestore.instance.collection("BusinessProfilesCollection");

  Future GetBusinessProfilesCollection() async {
    List businessprofileslist = [];
    try {
      var QuerySnapshot = await BusinessProfilesCollection.get();
      querySnapshot.docs.forEach((element) {
        businessprofileslist.add(element.data());
      });
      return businessprofileslist;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

Btw: returning null when the load fails, is just going to lead to a null pointer exception when you then do print(result.toString()); . So I recommend not catching the error and just letting it bubble up. With that your code can be simplified to:

class DatabaseManager {
  final CollectionReference BusinessProfilesCollection =
      FirebaseFirestore.instance.collection("BusinessProfilesCollection");

  Future GetBusinessProfilesCollection() async {
    var QuerySnapshot = await BusinessProfilesCollection.get();
    return querySnapshot.docs.map((element) => element.data());
  }
}

You just need to return the list

return businessprofileslist;

CODE:

class DatabaseManager {
  final CollectionReference BusinessProfilesCollection =
      FirebaseFirestore.instance.collection("BusinessProfilesCollection");

  Future GetBusinessProfilesCollection() async {
    List businessprofileslist = [];
    try {
      await BusinessProfilesCollection.get().then((QuerySnapshot) {
        QuerySnapshot.docs.forEach((element) {
          businessprofileslist.add(element.data());
          print(businessprofileslist[0]);
        });

        // you just need to return the list here after filling it up
        return businessprofileslist;

      });
    } catch (e) {
      print(e.toString());
      return null;
    }
  }
}

Code with a little improvement:

class DatabaseManager {
  final CollectionReference BusinessProfilesCollection =
      FirebaseFirestore.instance.collection("BusinessProfilesCollection");

  Future GetBusinessProfilesCollection() async {
    await BusinessProfilesCollection.get().then((QuerySnapshot) {
      QuerySnapshot.docs.map((doc) => doc.data()).toList();
    });
  }
}

Try that with calling the function in feching

    fetchBusinessProfilesList() 
    async {
       dynamic result ;
       await DatabaseManager().GetBusinessProfilesCollection().then((value){

       result=value;

      print(result.toString());
      if (result == null) {
       print('enable to retieve');
       } else {
       print('success');
       setState(() {
         businessprofileslist = result;
         });
         }
           });
 
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM