没有检索到特定集合 Firebase 的数据
No data retrieved for specific collection Firebase
我是 Flutter 的新手,我正在尝试构建一个送餐应用程序。
我设法使用提供商从 Firebase 加载类别,但完全相同的代码不适用于我的餐厅。
基本上,检索到的餐馆列表为空。它没有给我任何错误,仅此而已。
这是餐厅模特
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];
}
}
餐厅服务
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;
});
}
和餐厅供应商
class RestaurantProvider with ChangeNotifier{
RestaurantServices _restaurantServices = RestaurantServices();
List<RestaurantModel> restaurants = [];
RestaurantProvider.initialize(){
_loadRestaurants();
}
_loadRestaurants() async {
restaurants = await _restaurantServices.getRestaurants();
notifyListeners();
}
}
Future return 只是 FutureOr 所以我们只需要等待并从异步函数中获取值,如下所示
await _restaurantServices.getRestaurants().then((value){
restaurants = value;
notifyListeners();
});
希望对您的需求有所帮助
我是 Flutter 的新手,我正在尝试构建一个送餐应用程序。 我设法使用提供商从 Firebase 加载类别,但完全相同的代码不适用于我的餐厅。 基本上,检索到的餐馆列表为空。它没有给我任何错误,仅此而已。
这是餐厅模特
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];
}
}
餐厅服务
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;
});
}
和餐厅供应商
class RestaurantProvider with ChangeNotifier{
RestaurantServices _restaurantServices = RestaurantServices();
List<RestaurantModel> restaurants = [];
RestaurantProvider.initialize(){
_loadRestaurants();
}
_loadRestaurants() async {
restaurants = await _restaurantServices.getRestaurants();
notifyListeners();
}
}
Future return 只是 FutureOr 所以我们只需要等待并从异步函数中获取值,如下所示
await _restaurantServices.getRestaurants().then((value){
restaurants = value;
notifyListeners();
});
希望对您的需求有所帮助