- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我单击从个人资料页面到通知的通知导航项时显示此错误
Exception has occurred. FlutterError (setState() called afterdispose(): _MyProfileState#c3ad1(lifecycle state: defunct, notmounted) This error happens if you call setState() on a State objectfor a widget that no longer appears in the widget tree (e.g., whoseparent widget no longer includes the widget in its build). This errorcan occur when code calls setState() from a timer or an animationcallback. The preferred solution is to cancel the timer or stoplistening to the animation in the dispose() callback. Another solutionis to check the "mounted" property of this object before callingsetState() to ensure the object is still in the tree. This error mightindicate a memory leak if setState() is being called because anotherobject is retaining a reference to this State object after it has beenremoved from the tree. To avoid memory leaks, consider breaking thereference to this object during dispose().)
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:play_big_win/constants.dart';
import 'package:play_big_win/services/firebase-auth-helper.dart';
class MyProfile extends StatefulWidget {
@override
_MyProfileState createState() => _MyProfileState();
}
class _MyProfileState extends State<MyProfile> {
final firebaseUser = FirebaseAuth.instance.currentUser;
final db = FirebaseFirestore.instance;
String fullname = '';
String email = '';
@override
void dispose() {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}
@override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}
getData() async {
while (mounted) {
dynamic names = await FirebaseAuthHelper().getCurrentUserData();
if (names != null) {
fullname = names[0];
email = names[1];
setState(() {});
} else {
print("Nulllll");
}
}
}
@override
Widget build(BuildContext context) {
getData();
return Scaffold(
backgroundColor: kPrimaryColor,
body: SafeArea(
child: ListView(
shrinkWrap: true,
children: [
SizedBox(
height: 20,
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
width: MediaQuery.of(context).size.width - 25,
height: 150,
child: Row(
children: [
CircleAvatar(
radius: 60.0,
backgroundImage: NetworkImage(
'https://res.cloudinary.com/dqunmzmqo/image/upload/v1606248032/male-clipart-avatar_ewaerc.png',
),
),
Padding(
padding: const EdgeInsets.only(top: 45.0),
child: Column(
children: [
Text(
"Name: $fullname",
style: TextStyle(
fontSize: 14,
),
),
SizedBox(
height: 10,
),
Text(
"Email: $email",
style: TextStyle(
fontSize: 14,
),
),
SizedBox(
height: 10,
),
Text(
"Refer Code: Xasdsas",
style: TextStyle(
fontSize: 14,
),
)
],
),
),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
'Play Big Win Big',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 20.0,
color: Colors.white,
),
),
),
SizedBox(
height: 5,
),
Text(
'V1.0',
style: TextStyle(
fontSize: 10.0,
letterSpacing: 2.5,
color: Colors.teal.shade100,
fontWeight: FontWeight.bold,
),
),
SizedBox(
height: 20.0,
width: 150.0,
child: Divider(
color: Colors.teal.shade100,
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MyprivacyPolicy(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.group_add,
color: Colors.teal,
),
title: Text(
'Invite Friends',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MyprivacyPolicy(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.question_answer,
color: Colors.teal,
),
title: Text(
"FAQ's",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => MyprivacyPolicy(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.privacy_tip,
color: Colors.teal,
),
title: Text(
'Privacy Policy',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
GestureDetector(
onTap: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => TermsandCondtions(),
// ));
},
child: Card(
color: Colors.white,
margin:
EdgeInsets.symmetric(vertical: 10.0, horizontal: 25.0),
child: ListTile(
leading: Icon(
Icons.auto_fix_normal,
color: Colors.teal,
),
title: Text(
'Terms & Conditions',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 17.0,
color: Colors.teal.shade900,
),
),
),
),
),
],
),
],
),
),
);
}
}
通知页面:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../constants.dart';
class MyNotification extends StatefulWidget {
@override
_MyNotificationState createState() => _MyNotificationState();
}
class _MyNotificationState extends State<MyNotification> {
@override
void dispose() {
SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
super.dispose();
}
@override
initState() {
SystemChrome.setEnabledSystemUIOverlays([]);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
backgroundColor: kPrimaryColor,
title: Text(
"Notifications",
),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: Container(
decoration: BoxDecoration(
color: Colors.deepPurple,
borderRadius: BorderRadius.all(
Radius.circular(
20,
),
),
),
height: 300,
width: MediaQuery.of(context).size.width,
child: Center(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
"Today Is Your sadsadasdas das das dsa sa dsa dsad asd as das asd sad asd asds das sa dsa",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
),
);
}
}
我的验证码:
Future getCurrentUserData() async {
try {
DocumentSnapshot ds = await users.doc(firebaseUser.uid).get();
String fullname = ds.get('full_name');
String email = ds.get('email');
return [fullname, email];
} catch (e) {
print(e.toString());
return null;
}
}
我在个人资料页面中调用的这段代码:
getData() async {
while (mounted) {
dynamic names = await FirebaseAuthHelper().getCurrentUserData();
if (names != null) {
fullname = names[0];
email = names[1];
setState(() {});
} else {
print("Null");
}
}
}
最佳答案
您在 async 函数中使用 setState,等待后,您的小部件可能不再安装。做 setState 会在那个时候给你一个异常(exception)。使用以下代码或将其放在任何其他地方
if (this.mounted) {
setState(() {
});
}
或更明确的方法覆盖 setState
@override
void setState(fn) {
if(mounted) {
super.setState(fn);
}
}
关于flutter - 出现异常。 FlutterError (setState() 在 dispose() : _MyProfileState#c3ad1(lifecycle state: defunct, 未安装后调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65072009/
完整的错误如下错误在第2行 发生异常。 FlutterError(在初始化绑定(bind)之前访问了ServicesBinding.defaultBinaryMessenger。 如果您正在运行应用程
谁能告诉我如何在小部件测试期间实现覆盖 flutter 错误,以便我可以检查自己的自定义错误。 我在网上看到片段提到了这一点,但我所有的实现都失败了 void main() { testWidge
错误会在两个区域引发(并且应用程序卡住(当应用程序最小化,单击电话后退按钮或在flutter应用程序顶部运行另一个应用程序时。flutter版本:1.20.2(以前的版本没有此问题):这两个功能是:
当我单击从个人资料页面到通知的通知导航项时显示此错误 Exception has occurred. FlutterError (setState() called afterdispose(): _
我是一名优秀的程序员,十分优秀!