简体   繁体   English

没有检索到特定集合的数据 Firebase

[英]No data retrieved for specific collection Firebase

I am new to Flutter and I am trying to build a food delivery app.我是 Flutter 的新手,我正在尝试构建一个送餐应用程序。 I managed to load the categories from Firebase using a provider, but the exact same code wont work for my restaurants.我设法使用提供商从 Firebase 加载了类别,但完全相同的代码不适用于我的餐厅。 Basically, the list of restaurants retrieved is null. It gives me no errors, just that.基本上,检索到的餐厅列表是 null。它没有给我任何错误,仅此而已。

this is Restaurant Model这是餐厅 Model



class RestaurantModel{
  static const NAME = "name";
  static const ID = "id";
  static const AVG_PRICE = "avgPrice";
  static const RATING = "rating";
  static const RATES = "rates";
  static const IMAGE = "image";
  static const POPULAR = "popular";

  int _id;
  String _name;
  double _avgPrice;
  double _rating;
  String _image;
  bool _popular;
  int _rates;

  //GETTERS

  int get id => _id;
  String get name => _name;
  double get avgPrice => _avgPrice;
  double get rating => _rating;
  String get image => _image;
  bool get popular => _popular;
  int get rates => _rates;

  RestaurantModel.fromSnapshot(DocumentSnapshot snapshot){
    _id = snapshot.data()[ID];
    _name = snapshot.data()[NAME];
    _avgPrice = snapshot.data()[AVG_PRICE];
    _rating = snapshot.data()[RATING];
    _image = snapshot.data()[IMAGE];
    _popular = snapshot.data()[POPULAR];
    _rates = snapshot.data()[RATES];

  }

}

Restaurant services餐厅服务

    class RestaurantServices {

  String collection = "restaurants";
  FirebaseFirestore _firestore = FirebaseFirestore.instance;

  Future<List<RestaurantModel>> getRestaurants() async => _firestore.collection(collection).get().then((result){
    List<RestaurantModel> restaurants = [];
    for (DocumentSnapshot restaurant in result.docs) {
      restaurants.add(RestaurantModel.fromSnapshot(restaurant));
    }

    return restaurants;
  });



}

And Restaurant Provider和餐厅供应商

class RestaurantProvider with ChangeNotifier{

  RestaurantServices _restaurantServices = RestaurantServices();
  List<RestaurantModel> restaurants = [];

  RestaurantProvider.initialize(){
    _loadRestaurants();
  }

  _loadRestaurants() async {

    restaurants = await _restaurantServices.getRestaurants();
    notifyListeners();

  }

}

Future<List> is return only the FutureOr so we only need to wait and get the value from the async function like below Future<List> 只返回 FutureOr 所以我们只需要等待并从异步 function 中获取值,如下所示

 await _restaurantServices.getRestaurants().then((value){
      restaurants = value;
      notifyListeners();
    });

I hope it will help you to achieve your requirement.我希望它能帮助你实现你的要求。

I am new to Flutter and I am trying to build a food delivery app.我是 Flutter 的新手,我正在尝试构建一个送餐应用程序。 I managed to load the categories from Firebase using a provider, but the exact same code wont work for my restaurants.我设法使用提供程序从 Firebase 加载类别,但完全相同的代码不适用于我的餐厅。 Basically, the list of restaurants retrieved is null.基本上,检索到的餐馆列表是 null。 It gives me no errors, just that.它没有给我任何错误,仅此而已。

this is Restaurant Model这是餐厅 Model



class RestaurantModel{
  static const NAME = "name";
  static const ID = "id";
  static const AVG_PRICE = "avgPrice";
  static const RATING = "rating";
  static const RATES = "rates";
  static const IMAGE = "image";
  static const POPULAR = "popular";

  int _id;
  String _name;
  double _avgPrice;
  double _rating;
  String _image;
  bool _popular;
  int _rates;

  //GETTERS

  int get id => _id;
  String get name => _name;
  double get avgPrice => _avgPrice;
  double get rating => _rating;
  String get image => _image;
  bool get popular => _popular;
  int get rates => _rates;

  RestaurantModel.fromSnapshot(DocumentSnapshot snapshot){
    _id = snapshot.data()[ID];
    _name = snapshot.data()[NAME];
    _avgPrice = snapshot.data()[AVG_PRICE];
    _rating = snapshot.data()[RATING];
    _image = snapshot.data()[IMAGE];
    _popular = snapshot.data()[POPULAR];
    _rates = snapshot.data()[RATES];

  }

}

Restaurant services餐厅服务

    class RestaurantServices {

  String collection = "restaurants";
  FirebaseFirestore _firestore = FirebaseFirestore.instance;

  Future<List<RestaurantModel>> getRestaurants() async => _firestore.collection(collection).get().then((result){
    List<RestaurantModel> restaurants = [];
    for (DocumentSnapshot restaurant in result.docs) {
      restaurants.add(RestaurantModel.fromSnapshot(restaurant));
    }

    return restaurants;
  });



}

And Restaurant Provider和餐厅供应商

class RestaurantProvider with ChangeNotifier{

  RestaurantServices _restaurantServices = RestaurantServices();
  List<RestaurantModel> restaurants = [];

  RestaurantProvider.initialize(){
    _loadRestaurants();
  }

  _loadRestaurants() async {

    restaurants = await _restaurantServices.getRestaurants();
    notifyListeners();

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 更新检索到的数据 - Firebase updating the retrieved data 如何从 firebase 中的集合中的所有文档中获取特定数据? - How to get specific data from all documents from a collection in firebase? 从 firebase v9 检索到的数据未定义 - Retrieved data from firebase v9 is undefined RecyclerView 正在循环从 firebase 检索到的相同数据 - RecyclerView is looping the same data retrieved from firebase Firebase 特定集合的安全规则? - Firebase Security Rules to specific Collection? firebase 模拟器数据未保存并从某些缓存中检索 - firebase emulator data not saved and retrieved from some cache 如何获取从 Dart Flutter Firebase 检索到的数据的值? - How to get value of data retrieved from Dart Flutter Firebase? 怎么才能等到Firebase Flutter 取回数据库数据呢? - How can I wait until Firebase Database data is retrieved in Flutter? 从 firebase 检索到的数据中删除大括号和等号 - Remove curly braces and equal sign from data retrieved from firebase 有没有办法将从子集合 Firebase 中检索到的文档存储到一个数组中以显示其信息? AngularFirestore - Is there a way to store the retrieved documents from sub-collection Firebase into an array for displaying its info? AngularFirestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM