正在尝试访问用户的实例.小部件加载电子邮件时出现NoSuchMethod错误=null.调用loggedInUser.e



我正在调用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时引发了以下NoSuchMethodError(脏,状态:_SettingScreenState#b9d92(:getter"email"是在null上调用的。接收方:空尝试呼叫:电子邮件

我也经历过。我修复错误的方法是更改auth.currentUser((声明的位置。您可能已经在StatelessWidget中创建了Auth-Auth。

尝试将Auth实例从StatelessWidget移动到您的State,就在您的void initState((之前。

相关内容

最新更新