【问题标题】:type 'Future<dynamic>' is not a subtype of type '() => void' in flutter firestore类型 'Future<dynamic>' 不是 Flutter Firestore 中类型 '() => void' 的子类型
【发布时间】:2021-02-06 18:38:20
【问题描述】:

我正在尝试使用颤振在 Firestore 中创建这些字段,但我会不断收到此错误。我看到另一个人在这里发布了类似的问题,但他首选的解决方案并不能解决我的问题。我的代码摘录如下

                                          GestureDetector(
                                          onTap: (){
                                            return showDialog(
                                              context: context,
                                              builder: (context){
                                                return Center(
                                                  child: Material(
                                                    child: Padding(
                                                      padding: EdgeInsets.only(left: 20, right: 20, top: 10),
                                                      child: Container(
                                                        decoration: BoxDecoration(
                                                            borderRadius: BorderRadius.circular(10)
                                                        ),
                                                        height: 160,
                                                        width: 250,
                                                        child: Column(
                                                          children: [
                                                            Container(
                                                              decoration: BoxDecoration(
                                                                borderRadius: BorderRadius.circular(10)
                                                              ),
                                                              child: Form(
                                                                key: _mobiileKey,
                                                                autovalidate: _autoValidate,
                                                                child: TextFormField(
                                                                  maxLines: 1,
                                                                  autofocus: false,
                                                                  keyboardType: TextInputType.phone,
                                                                  onChanged: (value) {
                                                                    mobile = value;
                                                                  },
                                                                  validator: validateMobile,
                                                                  onSaved: (value) => mobile = value,
                                                                  style: TextStyle(
                                                                    color: Colors.black,
                                                                  ),
                                                                  decoration: InputDecoration(
                                                                      focusedBorder: OutlineInputBorder(
                                                                        borderRadius: BorderRadius.all(Radius.circular(4)),
                                                                        borderSide: BorderSide(width: 1,color: Palette.mainColor),
                                                                      ),
                                                                      border: OutlineInputBorder(),
                                                                      labelText: 'Phone Number',
                                                                      prefixIcon: Icon(Icons.phone_android,
                                                                        color: Colors.black,),
                                                                      labelStyle: TextStyle(
                                                                        fontSize: 15,
                                                                        color: Colors.black,
                                                                      )
                                                                  ),
                                                                ),
                                                              ),
                                                            ),
                                                            Padding(
                                                              padding: EdgeInsets.only(top: 10),
                                                              child: MaterialButton(
                                                                onPressed: validateAndSubmit(title, price, thumbnailUrl, mobile),
                                                                child: Text('PROCEED TO ORDER',
                                                                  style: TextStyle(
                                                                    fontSize: 15,
                                                                    fontWeight: FontWeight.bold,
                                                                  ),
                                                                ),
                                                                
                                                                color: Color(0xff706695),
                                                                elevation: 0,
                                                                minWidth: 400,
                                                                height: 50,
                                                                textColor: Colors.white,
                                                                shape: RoundedRectangleBorder(
                                                                    borderRadius: BorderRadius.circular(10)
                                                                ),
                                                              ),
                                                            ),
                                                          ],
                                                        ),
                                                      ),
                                                    )
                                                  )
                                                );
                                              }
                                            );
                                            print('Orders');
                                          }, 
                                        )

这就是 validateAndSubmit 函数

validateAndSubmit (title, price, thumbnailUrl, mobile) async {
    if (validateAndSave()){
      setState(() {
        loading = true;
      });

      try{
        Navigator.pushReplacement(context,
            MaterialPageRoute(builder: (context) => BottomNavScreen()));

        User user = auth.currentUser;

        await db.doc(widget.productId).set({
          'uid': user.uid,
          "Product Title": title,
          "Product Price": price,
          "Mobile Number": mobile,
          "thumbnail": thumbnailUrl,
          "Published Date": DateTime.now(),
        }).then((value) => null);

      } catch (e){

      }
    }
  }

这是错误消息

类型 'Future' 不是类型 '() => void' 的子类型

欢迎任何帮助。

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    改变:

    onPressed: validateAndSubmit(title, price, thumbnailUrl, mobile),
    

    到:

    onPressed:(){
      validateAndSubmit(title, price, thumbnailUrl, mobile);
    }
    

    我认为这可能是问题所在。 onPressed 需要void。让我知道这是否有效。

    【讨论】:

      猜你喜欢
      • 2020-01-14
      • 2021-02-10
      • 1970-01-01
      • 2019-08-18
      • 2023-03-30
      • 2020-12-09
      • 2019-12-05
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多