我在BottomNavigationBar
中有一个5菜单。当我从一个切换到另一个时,有时会收到类似
E/flutter (13314): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: setState() called after dispose(): _PromoProductState#a00c8(lifecycle state: defunct, not mounted)
E/flutter (13314): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
E/flutter (13314): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
E/flutter (13314): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
E/flutter (13314): #0 State.setState.<anonymous closure>
package:flutter/…/widgets/framework.dart:1112
E/flutter (13314): #1 State.setState
package:flutter/…/widgets/framework.dart:1147
E/flutter (13314): #2 _PromoProductState.initState.<anonymous closure>
package:tokopedia/widgets/promo_product.dart:16
E/flutter (13314): #3 _rootRun (dart:async/zone.dart:1122:38)
E/flutter (13314): #4 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (13314): #5 _CustomZone.runGuarded (dart:async/zone.dart:925:7)
E/flutter (13314): #6 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
E/flutter (13314): #7 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (13314): #8 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (13314): #9 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
E/flutter (13314): #10 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
E/flutter (13314): #11 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
E/flutter (13314): #12 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
E/flutter (13314): #13 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
仅当我从另一个菜单(快速)移至包含静态数据的home menu
时才发生这种情况(暂时不调用api)。
我认为这可能是代码的问题
@override
void initState() {
Provider.of<LoadingState>(context, listen: false).setLoading = 0;
Timer(Duration(seconds: 1), () {
setState(() {
time = time.add(Duration(seconds: -1));
});
});
super.initState();
listBottomWidget.add(HomeTab());
listBottomWidget.add(FeedTab());
listBottomWidget.add(InboxTab());
listBottomWidget.add(CartTab());
listBottomWidget.add(AccountTab());
}
但是我不确定
仅当我切换到HomeTab()
时才会显示错误消息。
这是我的家。飞镖
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:badges/badges.dart';
import 'package:provider/provider.dart';
import 'package:tokopedia/providers/loading_state.dart';
import 'package:tokopedia/view/tabs/tab_account.dart';
import 'package:tokopedia/view/tabs/tab_cart.dart';
import 'package:tokopedia/view/tabs/tab_feed.dart';
import 'package:tokopedia/view/tabs/tab_home.dart';
import 'package:tokopedia/view/tabs/tab_inbox.dart';
class HomePage extends StatefulWidget {
@override
State createState() => HomePageState();
}
class HomePageState extends State<HomePage> {
DateTime time = DateTime.now();
int selectedPosition = 0;
List<Widget> listBottomWidget = new List();
@override
void initState() {
Provider.of<LoadingState>(context, listen: false).setLoading = 0;
Timer(Duration(seconds: 1), () {
setState(() {
time = time.add(Duration(seconds: -1));
});
});
super.initState();
listBottomWidget.add(HomeTab());
listBottomWidget.add(FeedTab());
listBottomWidget.add(InboxTab());
listBottomWidget.add(CartTab());
listBottomWidget.add(AccountTab());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Row(
children: <Widget>[
Container(
padding: EdgeInsets.only(right: 12.0),
child: Image.asset(
"assets/images/qrcode.png",
width: 30,
height: 30,
),
),
Expanded(
child: Container(
padding: EdgeInsets.only(right: 8.0),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.all(
Radius.circular(5.0),
),
),
child: TextField(
decoration: const InputDecoration(
prefixIcon: Icon(
Icons.search,
color: Colors.grey,
),
border: InputBorder.none,
hintText: 'What you need?',
),
style: TextStyle(
fontSize: 16,
),
),
),
),
],
),
actions: <Widget>[
BadgeIconButton(
itemCount: 0,
icon: Icon(
Icons.favorite,
size: 28.0,
color: Colors.grey[400],
),
badgeColor: Colors.red,
badgeTextColor: Colors.white,
hideZeroCount: true,
onPressed: () {},
),
BadgeIconButton(
itemCount: 4,
icon: Icon(
Icons.notifications,
size: 28.0,
color: Colors.grey[400],
),
badgeColor: Colors.red,
badgeTextColor: Colors.white,
hideZeroCount: true,
onPressed: () {},
),
],
),
body: Builder(
builder: (context) {
return listBottomWidget[selectedPosition];
},
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: selectedPosition, // this will be set when a tab is tapped
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.book),
title: Text('Feed'),
),
BottomNavigationBarItem(
icon: Icon(Icons.mail),
title: Text('Inbox'),
),
BottomNavigationBarItem(
icon: Icon(Icons.shopping_cart),
title: Text('Cart'),
),
BottomNavigationBarItem(
icon: Icon(Icons.people),
title: Text('Account'),
),
],
onTap: (position) {
setState(() {
selectedPosition = position;
});
},
),
);
}
}
我想念什么吗?
谢谢
已编辑
我的家标签
import 'package:flutter/material.dart';
import 'package:tokopedia/widgets/banner_belanja_untung.dart';
import 'package:tokopedia/widgets/banner_beli_kebutuhan.dart';
import 'package:tokopedia/widgets/banner_promo_special.dart';
import 'package:tokopedia/widgets/image_slider.dart';
import 'package:tokopedia/widgets/kategori_pilihan.dart';
import 'package:tokopedia/widgets/menu_item.dart';
import 'package:tokopedia/widgets/most_trending_product.dart';
import 'package:tokopedia/widgets/promo_product.dart';
class HomeTab extends StatefulWidget {
@override
_HomeTabState createState() => _HomeTabState();
}
class _HomeTabState extends State<HomeTab> {
@override
Widget build(BuildContext context) {
var greyArea = Container(
height: 10.0,
color: Colors.grey[200],
);
return SingleChildScrollView(
child: Column(
children: <Widget>[
ImageSliderWidget(),
MenuItemWidget(),
greyArea,
PromoProduct(),
greyArea,
BannerPromoSpecial(),
KategoriPilihan(),
greyArea,
BannerBelanjaUntung(),
greyArea,
MostTrendingProduct(),
greyArea,
BannerBeliKebutuhan(),
greyArea,
KategoriPilihan(),
],
),
);
}
}
在我的PromoProduct
小部件内
import 'dart:async';
import 'package:flutter/material.dart';
class PromoProduct extends StatefulWidget {
@override
_PromoProductState createState() => _PromoProductState();
}
class _PromoProductState extends State<PromoProduct> {
DateTime time = DateTime.now();
@override
void initState() {
Timer(Duration(seconds: 1), () {
setState(() {
time = time.add(Duration(seconds: -1));
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
var promoProductItems = [
{
"image": "assets/images/product/tas.jpg",
"real_price": "Rp 249.000",
"discount_price": "Rp 87.150",
"discount_percent": "65",
},
{
"image": "assets/images/product/qtela.jpg",
"real_price": "Rp 25.000",
"discount_price": "Rp 20.000",
"discount_percent": "20",
},
{
"image": "assets/images/product/tropicana_slim.jpg",
"real_price": "Rp 83.500",
"discount_price": "Rp 58.500",
"discount_percent": "30",
},
{
"image": "assets/images/product/pipo_frixion.jpg",
"real_price": "Rp 27.000",
"discount_price": "Rp 16.200",
"discount_percent": "40",
},
{
"image": "assets/images/product/bioderma_sebium.jpg",
"real_price": "Rp 252.625",
"discount_price": "Rp 155.000",
"discount_percent": "39",
},
{
"image": "assets/images/product/maxim_new_prestige.jpg",
"real_price": "Rp 612.000",
"discount_price": "Rp 263.000",
"discount_percent": "57",
},
];
return Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Text("Flash Sale ",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.bold,
)),
Container(
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Color(0xffff3e2f),
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
child: Text(
time.hour.toString().padLeft(2, "0"),
style: TextStyle(
color: Colors.white,
),
),
),
Text(
" : ",
style: TextStyle(
color: Color(0xffff3e2f),
),
),
Container(
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Color(0xffff3e2f),
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
child: Text(
time.minute.toString().padLeft(2, "0"),
style: TextStyle(
color: Colors.white,
),
),
),
Text(
" : ",
style: TextStyle(
color: Color(0xffff3e2f),
),
),
Container(
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: Color(0xffff3e2f),
borderRadius: BorderRadius.all(Radius.circular(6.0)),
),
child: Text(
time.second.toString().padLeft(2, "0"),
style: TextStyle(
color: Colors.white,
),
),
),
],
),
),
Text("Lihat Semuanya",
style: TextStyle(
color: Colors.green,
)),
],
),
),
Container(
width: MediaQuery.of(context).size.width,
height: 280.0,
child: IgnorePointer(
child: GridView.count(
crossAxisCount: 3,
children: List.generate(
promoProductItems.length,
(index) {
return Column(
children: <Widget>[
Stack(
children: <Widget>[
Container(
height: 85.0,
child: Image.asset(
promoProductItems[index]["image"],
),
),
Positioned(
top: 0,
right: 0,
child: Container(
padding: EdgeInsets.all(4.0),
child: Text(
"${promoProductItems[index]["discount_percent"]} OFF",
style: TextStyle(
color: Colors.red[900],
fontSize: 10.0,
fontWeight: FontWeight.bold,
),
),
decoration: BoxDecoration(
color: Colors.red[100],
border: Border.all(
color: Colors.red,
width: 2.0,
),
borderRadius: BorderRadius.all(
Radius.circular(12.0),
),
),
),
),
],
),
Text(
promoProductItems[index]["real_price"],
style: TextStyle(
decoration: TextDecoration.lineThrough,
),
),
Text(
promoProductItems[index]["discount_price"],
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.orange[900],
),
),
],
);
},
),
),
),
),
],
);
}
}
答案 0 :(得分:4)
这不是完全正确的方法,但是有时您将需要使用它。喜欢你的问题。定义一个布尔并在放置小部件时对其进行更改。并且不要在没有它的情况下使用任何setState
。例如;
DateTime time = DateTime.now();
bool _disposed = false;
@override
void initState() {
Timer(Duration(seconds: 1), () {
if (!_disposed)
setState(() {
time = time.add(Duration(seconds: -1));
});
});
super.initState();
}
@override
void dispose() {
_disposed = true;
super.dispose();
}
答案 1 :(得分:1)
在您的其中一个标签中,您使用了一些触发setState
方法的代码。可能在将来的功能中。请在标签内分享,让您在离开该错误时会收到此错误。