flutter - 在Flutter Firestore中, 'Future<dynamic>'类型不是 '() => void'类型的子类型

标签 flutter dart google-cloud-firestore

我正在尝试使用flutter在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'的子类型
欢迎任何帮助。

最佳答案

更改:

onPressed: validateAndSubmit(title, price, thumbnailUrl, mobile),
至:
onPressed:(){
  validateAndSubmit(title, price, thumbnailUrl, mobile);
}
我认为这可能是问题所在。 onPressed需要一个void。让我知道这个是否奏效。

关于flutter - 在Flutter Firestore中, 'Future<dynamic>'类型不是 '() => void'类型的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64504371/

相关文章:

ios - 无法使用类型 'String' 的参数列表调用类型 '(RemoteConfigValue)' 的初始值设定项

flutter - 向下滚动时隐藏底部导航栏,反之亦然

flutter - MappedListIterable 不是 SubType

flutter - 如何将共享功能添加到 flutter 中的图标[在社交媒体中单击按钮共享应用程序]

flutter - 我需要知道用户单击按钮时是否按下(按下)键

android - Firebase 函数在部署时抛出异常

python - Firestore 中具有服务帐户的细化权限

dart - Flutter 在页面中获取导航器参数

firebase - "Method not found: ' guardWebExceptions '"编译Flutter web应用时

Flutter 图表插件