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

时间:2021-05-07 14:15:02

标签: flutter dynamic future

======== 小部件库捕获的异常=============================== = 构建 ProfileEdit(dirty, state: _ProfileEditState#eb2ff) 时抛出了以下 _TypeError: '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;
              },
            ),
          ],
        ),
      ),
    );
  }
}
 

2 个答案:

答案 0 :(得分:1)

代替

_save() async {

尝试使用

Future _save() async {

答案 1 :(得分:1)

按钮的 onPressed 属性需要一个返回类型为 void 的函数,并且每个标记为 async 的函数总是具有某种类型的返回类型Future。为了解决这个问题,您可以将 _save 传递给 lambda 函数中的按钮:

onPressed: () => _save(),