【问题标题】:There's no error but I am not getting data from my firestore database while using GetX flutter没有错误,但在使用 GetX Flutter 时我没有从我的 Firestore 数据库中获取数据
【发布时间】:2021-09-16 14:31:54
【问题描述】:

控制器工作正常,一切正常。但是,我无法显示添加到 Firestore 数据库中的数据。

下面是显示数据的小部件。

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(),
        ));
  }
}

下面是 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());
}

下面是产品模型

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];
  }
}

这是 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"

【问题讨论】:

    标签: firebase flutter google-cloud-firestore flutter-getx


    【解决方案1】:
    Stream<List<ProductModel>> getAllProducts() async{
         await firebaseFirestore.collection(collection).snapshots().map((query) =>
              query.docs.map((item) => ProductModel.fromMap(item.data())).toList());
    }
    

    用这个替换你的 getallproducts 函数。

    【讨论】:

    • 非常感谢,Prashant。但是当我尝试调用 getAllProducts 时出现错误。参数类型“Future>>”不能分配给参数类型“Stream>
    • Future>> getAllProducts() async{ await firebaseFirestore.collection(collection).snapshots().map((query) => query.docs.map((item ) => ProductModel.fromMap(item.data())).toList()); } 试试这个
    • 再次感谢您,我已将返回类型添加为 Future 但是当我使用此代码将产品绑定到流时 products.bindStream(getAllProducts());它将 getAllProducts() 显示为下划线错误并显示此错误:参数类型 'Future>>' 不能分配给参数类型 'Stream>
    • 我尝试过的另一个选项是删除 Future 作为返回类型并使用 async* 但它也不显示任何内容。代码如下所示; Stream> getAllProducts() async* { await firebaseFirestore.collection(collection).snapshots().map((query) => query.docs.map((item) => ProductModel.fromMap(item.data ())).toList()); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2021-03-11
    • 1970-01-01
    • 2021-09-28
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多