[英]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.