尝试访问用户实例。小部件加载email = null时,出现NoSuchMethod错误。呼叫loggingInUser.email

时间:2019-10-25 02:09:12

标签: firebase flutter dart

我正在呼叫Firebase用户电子邮件,我想在屏幕上显示用户详细信息。加载小部件时,会显示NoSuchMethod错误并使应用程序崩溃。不确定从这里去哪里

import 'package:flutter/material.dart';
import 'package:simmanager/constaints.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class SettingScreen extends StatefulWidget {
  @override
  _SettingScreenState createState() => _SettingScreenState();
}

class _SettingScreenState extends State<SettingScreen> {
  final _auth = FirebaseAuth.instance;
  final _firestore = Firestore.instance;
  FirebaseUser loggedInUser;


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

  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser();
      if (user != null) {
        loggedInUser = user;
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Settings'),
        backgroundColor: primaryColor,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: Icon(
          Icons.edit,
          color: Colors.black,
        ),
        backgroundColor: secondaryColor,
      ),
      body: SafeArea(
        child: Container(
          color: backgroundColor,
          child: Column(
            children: <Widget>[
              Center(
                child: Container(
                  margin: EdgeInsets.only(top: 20.0),
                  child: Icon(
                    Icons.account_circle,
                    size: 75.0,
                    color: primaryLight,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        blurRadius: 5.0,
                        color: Colors.grey[300],
                        spreadRadius: 5.0,
                      ),
                    ],
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                  padding: EdgeInsets.all(25.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text(
                          'Name: ',
                          style: TextStyle(fontSize: 16.0),
                        ),
                      ),
                      // TODO: add stream for user details
                      Container(
                        child: Text('Test'),
                      ),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(10.0),
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        blurRadius: 5.0,
                        color: Colors.grey[300],
                        spreadRadius: 5.0,
                      ),
                    ],
                    borderRadius: BorderRadius.circular(20.0),
                  ),
                  padding: EdgeInsets.all(25.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        child: Text(
                          'Email: ',
                          style: TextStyle(fontSize: 16.0),
                        ),
                      ),
                      // TODO: add stream for user details
                      Container(
                        child: Text(loggedInUser.email),
                      ),
                    ],
                  ),
                ),
              ),

在构建SettingScreen(脏,状态:_SettingScreenState#b9d92)时引发了以下NoSuchMethodError: getter'email'被调用为null。 接收者:null 尝试致电:电子邮件

1 个答案:

答案 0 :(得分:-1)

我也通过了它。我设法解决错误的方法是更改​​auth.currentUser()声明的位置。您可能已经在StatelessWidget中创建了Auth auth。

在无效的initState()之前,尝试将Auth实例从StatelessWidget移到您的州。