类型 'Future<dynamic>' 不是类型 '() => void' 的子类型
type 'Future<dynamic>' is not a subtype of type '() => void' flutter
════════ 组件库捕获异常═════════════════════════════════ ═══
以下 _TypeError 被抛出构建 ProfileEdit(脏,状态:_ProfileEditState#eb2ff):
类型 'Future' 不是类型 '() => void'
的子类型
请告诉我我哪里出错了,我是 Flutter 的新手。
你能帮忙解决这个问题吗?
class ProfileEdit extends StatefulWidget {
final Users profile;
const ProfileEdit({Key key, this.profile}) : super(key: key);
@override
_ProfileEditState createState() => _ProfileEditState();
}
class _ProfileEditState extends State<ProfileEdit> {
var _formKey = GlobalKey<FormState>();
String _userName;
File _valuePhoto;
bool _loading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey,
title: Text(
"Profili Düzenle",
style: TextStyle(color: Colors.black),
),
leading: IconButton(
icon: Icon(
Icons.close,
color: Colors.black,
),
onPressed: () => Navigator.pop(context)),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.check,
color: Colors.black,
),
onPressed: _save()),
],
),
body: ListView(
children: <Widget>[
_loading
? LinearProgressIndicator()
: SizedBox(
height: 0.0,
),
_profilePhoto(),
_userInfo(),
],
),
);
}
_save() async {
if (_formKey.currentState.validate()) {
setState(() {
_loading = true;
});
_formKey.currentState.save();
String profilePhotoUrl;
if (_valuePhoto == null) {
profilePhotoUrl = widget.profile.photoUrl;
} else {
profilePhotoUrl = await StorageService().profilePhotoAdd(_valuePhoto);
}
String activeUserId =
Provider.of<AuthorizationService>(context, listen: false)
.activeUserId;
//veritabanına güncel verileri eklemek için.
FireStoreService().userUpdate(
userId: activeUserId, userName: _userName, photoUrl: profilePhotoUrl);
setState(() {
_loading = false;
});
Navigator.pop(context);
}
}
_profilePhoto() {
return Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 20.0),
child: Center(
child: InkWell(
onTap: _galleryChoose,
child: CircleAvatar(
backgroundColor: Colors.grey,
backgroundImage: _valuePhoto == null
? NetworkImage(widget.profile.photoUrl)
: FileImage(_valuePhoto), //galeriden fotoğraf ekleme.
radius: 55.0,
),
),
),
);
}
_galleryChoose() async {
var image = await ImagePicker().getImage(
source: ImageSource.gallery,
maxWidth: 800,
maxHeight: 600,
imageQuality: 80);
setState(() {
_valuePhoto = File(image.path);
});
}
_userInfo() {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 120.0,
),
child: Form(
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
TextFormField(
//varsayılan kullanıcı adı düzenleme ekranında da gözükmesi için initialvalue çalışıyor.
initialValue: widget.profile.userName,
decoration: InputDecoration(labelText: "Kullanıcı Adı"),
validator: (inputValue) {
return inputValue.trim().length <= 3
? "Kullanıcı Adı en az 4 karakter olmalı."
: null;
},
onSaved: (inputValue) {
_userName = inputValue;
},
),
],
),
),
);
}
}
而不是
_save() async {
尝试使用
Future _save() async {
按钮的 onPressed
属性 需要一个 return 类型为 void
的函数,并且每个函数都被标记为 async
总会有某种 Future
的 return 类型。为了解决这个问题,您可以将 _save
传递给 lambda 函数中的按钮:
onPressed: () => _save(),
════════ 组件库捕获异常═════════════════════════════════ ═══ 以下 _TypeError 被抛出构建 ProfileEdit(脏,状态:_ProfileEditState#eb2ff): 类型 'Future' 不是类型 '() => void'
的子类型请告诉我我哪里出错了,我是 Flutter 的新手。
你能帮忙解决这个问题吗?
class ProfileEdit extends StatefulWidget {
final Users profile;
const ProfileEdit({Key key, this.profile}) : super(key: key);
@override
_ProfileEditState createState() => _ProfileEditState();
}
class _ProfileEditState extends State<ProfileEdit> {
var _formKey = GlobalKey<FormState>();
String _userName;
File _valuePhoto;
bool _loading = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey,
title: Text(
"Profili Düzenle",
style: TextStyle(color: Colors.black),
),
leading: IconButton(
icon: Icon(
Icons.close,
color: Colors.black,
),
onPressed: () => Navigator.pop(context)),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.check,
color: Colors.black,
),
onPressed: _save()),
],
),
body: ListView(
children: <Widget>[
_loading
? LinearProgressIndicator()
: SizedBox(
height: 0.0,
),
_profilePhoto(),
_userInfo(),
],
),
);
}
_save() async {
if (_formKey.currentState.validate()) {
setState(() {
_loading = true;
});
_formKey.currentState.save();
String profilePhotoUrl;
if (_valuePhoto == null) {
profilePhotoUrl = widget.profile.photoUrl;
} else {
profilePhotoUrl = await StorageService().profilePhotoAdd(_valuePhoto);
}
String activeUserId =
Provider.of<AuthorizationService>(context, listen: false)
.activeUserId;
//veritabanına güncel verileri eklemek için.
FireStoreService().userUpdate(
userId: activeUserId, userName: _userName, photoUrl: profilePhotoUrl);
setState(() {
_loading = false;
});
Navigator.pop(context);
}
}
_profilePhoto() {
return Padding(
padding: const EdgeInsets.only(top: 15.0, bottom: 20.0),
child: Center(
child: InkWell(
onTap: _galleryChoose,
child: CircleAvatar(
backgroundColor: Colors.grey,
backgroundImage: _valuePhoto == null
? NetworkImage(widget.profile.photoUrl)
: FileImage(_valuePhoto), //galeriden fotoğraf ekleme.
radius: 55.0,
),
),
),
);
}
_galleryChoose() async {
var image = await ImagePicker().getImage(
source: ImageSource.gallery,
maxWidth: 800,
maxHeight: 600,
imageQuality: 80);
setState(() {
_valuePhoto = File(image.path);
});
}
_userInfo() {
return Padding(
padding: const EdgeInsets.symmetric(
horizontal: 120.0,
),
child: Form(
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
TextFormField(
//varsayılan kullanıcı adı düzenleme ekranında da gözükmesi için initialvalue çalışıyor.
initialValue: widget.profile.userName,
decoration: InputDecoration(labelText: "Kullanıcı Adı"),
validator: (inputValue) {
return inputValue.trim().length <= 3
? "Kullanıcı Adı en az 4 karakter olmalı."
: null;
},
onSaved: (inputValue) {
_userName = inputValue;
},
),
],
),
),
);
}
}
而不是
_save() async {
尝试使用
Future _save() async {
按钮的 onPressed
属性 需要一个 return 类型为 void
的函数,并且每个函数都被标记为 async
总会有某种 Future
的 return 类型。为了解决这个问题,您可以将 _save
传递给 lambda 函数中的按钮:
onPressed: () => _save(),