我正在尝试使用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/