繁体   English   中英

如何使用 flutter 向 firebase 添加新数据?

[英]how to add new data to firebase with flutter?

我正在尝试使用 flutter 发布帖子,但我正在使用此代码,但它不起作用

 final CollectionReference _productCollectionRef =
      FirebaseFirestore.instance.collection('Cars');

  Future<void> addCarsToFireStore(ProductModel productModel) async {
    return await _productCollectionRef.doc().set(productModel.toJson());
  }

  Future addCars() async {
    return await _productCollectionRef.add({
      'name': name,
      'image': image,
      'description': description,
      'price': price,
      'country': box.read(NAME_OF_CONTRY),
      'number': number,
      'uid': box.read('uid'),
    });
  }

我在 UI 中使用这段代码

CustomTextFormFild(
                    onSave: (value) {
                      value == null
                          ? Text('Valid value')
                          : value = controller.name;
                    },)

我对所有内容都这样做我也有这个来保存图片

 CustomButton(
                        onPressed: () {
                          controller.getImage(ImageSource.gallery);

                          controller.image = controller.selectedImagePath.value;
                        },
                        text: 'upload image',
                        alignment: Alignment.center,
                      ),

我想用这个按钮发帖

CustomButton(
                      onPressed: () {
                        controller.addCars();
                      },
                      text: 'add post',
                      alignment: Alignment.center,
                    ),

我希望有人帮助我

您能否尝试等待:

CustomButton(
onPressed: () async  {
  await controller.addCars();
},
text: 'add post',
alignment: Alignment.center,
),

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM