【问题标题】:Flutter The method 'then' isn't defined for the type 'User'Flutter 没有为“User”类型定义“then”方法
【发布时间】:2021-08-21 10:58:26
【问题描述】:

我正在创建一个使用 Firebase 作为后端的应用程序。最初代码工作正常,但从 firebase 更新后,我遇到了问题。问题是:没有为“用户”类型定义方法“then”。

void _currentUser() async {
    await _auth.currentUser.then((user) {
      setState(() {
        _email = user.email;
      });
    });
    setState(() {
      _isLoading = false;
    });
  }

页面代码

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:loading_overlay/loading_overlay.dart';
import 'package:rflutter_alert/rflutter_alert.dart';

class HomePage extends StatefulWidget {
  final String title;
  HomePage(this.title);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  FirebaseAuth _auth = FirebaseAuth.instance;
  String _email;
  bool _isLoading = true;

  @override
  void initState() {
    super.initState();
    _currentUser();
  }

  @override
  Widget build(BuildContext context) {
    return LoadingOverlay(
      isLoading: _isLoading,
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
          actions: <Widget>[
            RaisedButton.icon(
              label: Text(
                "Çıkış Yap",
                style: TextStyle(color: Colors.white),
              ),
              color: Colors.red,
              icon: Icon(
                Icons.exit_to_app,
                size: 32,
                color: Colors.white,
              ),
              onPressed: _userCikis,
            )
          ],
        ),
        body: Center(
            child: Text(
          "Hoşgeldiniz! \n $_email",
          style: TextStyle(fontSize: 24),
          textAlign: TextAlign.center,
        )),
      ),
    );
  }

  void _currentUser() async {
    await _auth.currentUser.then((user) {
      setState(() {
        _email = user.email;
      });
    });
    setState(() {
      _isLoading = false;
    });
  }

  void _userCikis() async {
    await _auth
        .signOut()
        .then((value) => Navigator.pushNamed(context, "/"))
        .catchError((onError) {
      Alert(
          type: AlertType.warning,
          context: context,
          title: "ÇIKIŞ YAPILAMADI!",
          buttons: [
            DialogButton(
              child: Text(
                "KAPAT",
                style: TextStyle(color: Colors.white, fontSize: 24),
              ),
              onPressed: () => Navigator.pop(context),
            )
          ]).show();
    });
  }
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    _auth.currentUser 正在返回 User 而不是 Future&lt;User&gt;

    这是您的更新代码

    void _currentUser() {
              final User? user = _auth.currentUser;
              /// final User user = _auth.currentUser; without null safety 
              setState(() {
                _email = user.email;
              });
    
              setState(() {
                _isLoading = false;
              });
            }
    

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 2021-02-09
      • 2020-12-15
      • 2020-10-01
      • 2021-10-04
      • 2019-08-14
      • 2020-10-23
      • 1970-01-01
      • 2021-09-02
      相关资源
      最近更新 更多