简体   繁体   English

没有错误,但我在使用 GetX flutter 时没有从我的 Firestore 数据库中获取数据

[英]There's no error but I am not getting data from my firestore database while using GetX flutter

Controllers are working right and so is everything.控制器工作正常,一切正常。 However, I am not able to display data that I added to my firestore database.但是,我无法显示添加到 Firestore 数据库中的数据。

Below is the widget to display the data.下面是显示数据的小部件。

class ProductsWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Obx(() => GridView.count(
          crossAxisCount: 2,
          childAspectRatio: .63,
          padding: EdgeInsets.all(10),
          mainAxisSpacing: 4,
          crossAxisSpacing: 10,
          children: productsController.products.map((ProductModel product) {
            return Text(product.name);
          }).toList(),
        ));
  }
}

Below is the productsController object下面是 productsController 对象

class ProductsController extends GetxController {
  static ProductsController instance =
      Get.find(); //creating an instance of Get.find. it returns the controller

  //defining a list of products to be displayed on the app. 
  RxList<ProductModel> products = RxList<ProductModel>([]);

  //name of collection to access on the database
  String collection = 'products';

  
  @override
  void onReady() {
    super.onReady();

    //binding the products into a stream
    products.bindStream(getAllProducts());
  }

  Stream<List<ProductModel>> getAllProducts() =>
      firebaseFirestore.collection(collection).snapshots().map((query) =>
          query.docs.map((item) => ProductModel.fromMap(item.data())).toList());
}

Below is the ProductModel下面是产品型号

class ProductModel {
  static const ID = 'id';
  static const NAME = 'name';
  static const IMAGE = 'image';
  static const PRICE = 'prep_time';
  static const PREP_TIME = 'prep_time';
  static const COOKING_TIME = 'cooking_time';
  static const SERVES = 'serves';
  static const DESCRIPTION = 'description';
  static const INGREDIENTS = 'ingredients';
  static const METHOD = 'method';

  String id;
  String name;
  Image image;
  String price;
  String prep_time;
  String cooking_time;
  String serves;
  String description;
  List<String> ingredients;
  List<String> method;

  ProductModel(
      {this.id,
      this.name,
      this.image,
      this.price,
      this.prep_time,
      this.cooking_time,
      this.serves,
      this.description,
      this.ingredients,
      this.method});

  ProductModel.fromMap(Map<String, dynamic> data) {
    id = data[ID];
    name = data[NAME];
    image = data[IMAGE];
    price = data[PRICE];
    prep_time = data[PREP_TIME];
    cooking_time = data[COOKING_TIME];
    serves = data[SERVES];
    description = data[DESCRIPTION];
    ingredients = data[INGREDIENTS];
    method = data[METHOD];
  }
}

Here is a view of the data in the firestore database这是firestore数据库中数据的视图

cooking_time
"1 hr and 5 mins"
(string)
description
"Make this easy chipolata, bean and roasted veg one-pan dish for a healthy, flavour-packed meal that the whole family will love. It offers four of your five-a-day "
id
"69lWeEFzCYhqDgHuTOYu"
image
"https://firebasestorage.googleapis.com/v0/b/seddi-89190.appspot.com/o/sausage.jpg?alt=media&token=e3c5a3af-b17a-4cfe-871e-d81797cc851b"
ingredients
0
"1 red or yellow pepper, deseeded and cut into chunks"
1
"2 carrots, cut into thick slices"
2
"2 red onions, cut into wedges"
3
"8 chipolatas, cut into thirds"
4
"400g can peeled cherry tomatoes"
5
"400g can white beans, drained"
6
"200ml low-salt chicken stock"
7
"2 tsp Dijon mustard"
8
"100g frozen peas"
9
"potatoes, pasta or rice, to serve"
method
0
"STEP 1: Heat oven to 220C/200C fan/gas 7. Roast the pepper, carrots and onion in a deep baking dish for 15 mins. Add the sausages and roast for a further 10 mins."
1
"STEP 2: Reduce oven to 200C/180C fan/gas 6, tip in the tomatoes and beans, then stir in the stock. Cook for another 35 mins. Stir in the mustard and peas and return to the oven for 5 mins. Rest for 10 mins, then serve with potatoes, pasta or rice"
name
"Sausage & white bean casserole"
prep_time
"20 mins"
price
"20000"
serves
"4"
Stream<List<ProductModel>> getAllProducts() async{
     await firebaseFirestore.collection(collection).snapshots().map((query) =>
          query.docs.map((item) => ProductModel.fromMap(item.data())).toList());
}

replace your getallproducts function with this.用这个替换你的 getallproducts 函数。

暂无
暂无

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

相关问题 我在尝试将数据从云 Firestore 带到我的 flutter 应用程序时遇到此错误 - I am getting this error while am trying to bring data from cloud firestore to my flutter app 将日期数据上传到 flutter 中的 firestore 时出现错误 - I am getting error while uploading date data to firestore in flutter 从firestore(firebase)获取数据时出现错误。 我正在使用本机反应 - I am getting an error while fetching data from firestore(firebase). I am using react native 使用 Flutter Firebase Firestore 从数据库获取地理点数据 - Getting Geopoint Data from Database Using Flutter Firebase Firestore 当我在 class Android Studio 中从 firestore 获取数据时,为什么我的应用程序出错 - Why does my app error when I am Getting data from firestore in class Android Studio 在Angular中将数据发送到Firebase数据库时出现错误 - I am getting error While sending data to the firebase Database in Angular 我正在尝试使用 getter 和 setter 从我的 firebase 数据库中检索数据,但收到错误提示无法转换对象 - I am trying to retrieve data from my firebase database using getter and setter but getting an error says can't convert the object Flutter:我正在尝试从 Firestore 读取数据中的 URL - Flutter: I am trying to read the URL in the data from the Firestore 我正在尝试从ios Swift项目中的Firestore数据库中获取数据 - I am trying to fetch data from Firestore database in my ios Swift Project 我收到“预期有 1 个位置参数,但找到 0 个”。 使用 Dart 和 Cloud Firestore 在 Flutter 中出错? - I'm getting a "1 positional argument(s) expected, but 0 found." error in Flutter using Dart with Cloud Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM