错误在两个区域引发(并且应用程序冻结(当应用程序最小化,单击电话返回按钮时,或者在flutter应用程序顶部运行另一个应用程序时。Flutter版本:1.20.2(先前版本没有这个问题):这两个功能是:
@override
void initState() {
super.initState();
getItems();
}
getItems() async {
initClearVisibility();
initFilters();
setState(() {
loadingItems = true;
Visibility(visible: true, child: CircularProgressIndicator());
});
QuerySnapshot querySnapshot = await query.get();
items = querySnapshot.docs;
lastDocument = querySnapshot.docs[querySnapshot.docs.length - 1];
setState(() {
loadingItems = false;
Visibility(visible: false, child: CircularProgressIndicator());
});
}
initClearVisibility() {
if (Str.filterSelectCategory != Str.CATEGORY) {
clearCategoryVisible = true;
allCategoriesVisible = false;
categoryValue = Str.filterSelectCategory;
setState(() {});
}
}
initFilters() async {
filterDefaultItems();
}
filterDefaultItems() async {
query = _firestore
.collection(Str.ITEMS)
.where(Str.IS_ITEM_SOLD, isEqualTo: false)
.where(Str.ADDRESS, isEqualTo: userAddress1)
//.orderBy(Str.DATE_POSTED)
.limit(perPage);
}
第二个区域也位于以下代码中::
class FABBottomAppBarItem {
FABBottomAppBarItem({this.iconData, this.itemColor}); //, this.text});
IconData iconData;
var itemColor;
//String text;
}
class FABBottomAppBar extends StatefulWidget {
FABBottomAppBar({
this.items,
this.centerItemText,
this.height: 65.0,
this.iconSize: 24.0,
this.backgroundColor,
this.color,
this.selectedColor,
this.notchedShape,
this.onTabSelected,
}) {
assert(this.items.length == 2 || this.items.length == 4);
}
final List<FABBottomAppBarItem> items;
final String centerItemText;
final double height;
final double iconSize;
final Color backgroundColor;
final Color color;
final Color selectedColor;
final NotchedShape notchedShape;
final ValueChanged<int> onTabSelected;
@override
State<StatefulWidget> createState() => FABBottomAppBarState();
}
class FABBottomAppBarState extends State<FABBottomAppBar> {
//int _selectedIndex = 0;
int unreadCount = 0;
_updateIndex(int index) {
widget.onTabSelected(index);
setState(() {
//_selectedIndex = index;
});
}
@override
void initState() {
super.initState();
countDocuments();
}
@override
Widget build(BuildContext context) {
List<Widget> items = List.generate(widget.items.length, (int index) {
return _buildTabItem(
item: widget.items[index],
index: index,
onPressed: _updateIndex,
);
});
items.insert(items.length >> 1, _buildMiddleTabItem());
return BottomAppBar(
shape: widget.notchedShape,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: items,
),
color: widget.backgroundColor,
);
}
Widget _buildMiddleTabItem() {
return Expanded(
child: SizedBox(
height: MediaQuery.of(context).size.height * 0.075, //widget.height,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: MediaQuery.of(context).size.height * 0.04,
),
Text(
widget.centerItemText ?? '',
style: TextStyle(
color: BwerereTheme.bwerereRed,
fontSize: 14.0,
fontWeight: FontWeight.w900),
),
],
),
),
);
}
Widget _buildTabItem({
FABBottomAppBarItem item,
int index,
ValueChanged<int> onPressed,
})
{
return Expanded(
child: SizedBox(
height: MediaQuery.of(context).size.height * 0.065,
child: Material(
type: MaterialType.transparency,
child: InkWell(
onTap: () => onPressed(index),
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Stack(
children: <Widget>[
Icon(item.iconData,
color: item.itemColor,
size: IconTheme.of(context).size * 1.2),
index == 2 ? badge() : Container()
],
)
],
),
),
),
),
);
}
Widget badge() => unreadCount < 1
? Container()
: Container(
padding: EdgeInsets.all(4.0),
decoration: BoxDecoration(
color: BwerereTheme.bwerereRed, shape: BoxShape.circle),
child: Center(
child: RobotoFont(
text: "$unreadCount",
textSize: 12.0,
textColor: Colors.white,
fontWeight: FontWeight.w400),
));
void countDocuments() async {
final uid = await FetchUserData().getCurrentUserID();
QuerySnapshot _myDoc = await FirebaseFirestore.instance
.collection("userUnreadMessages")
.doc(uid)
.collection(Str.MESSAGE_COLLECTION)
.get();
List<DocumentSnapshot> _myDocCount = _myDoc.docs;
setState(() {
unreadCount = _myDocCount.length;
print('NOTIY LENGTH::: $unreadCount');
});
}
}
对于FABBottomAppBarState,来自FRAMEWORK.DART的错误。
在HomePage()的getItems上抛出相同的错误
发生异常。 FlutterError(在dispose()之后调用setState():FABBottomAppBarState#250ac(生命周期状态:已失效,未安装) 如果您在不再出现在小部件树中的小部件的State对象上调用setState(),则会发生此错误(例如,其父小部件不再在其构建中包含该小部件)。当代码从计时器或动画回调中调用setState()时,可能会发生此错误。 首选解决方案是取消计时器或停止收听dispose()回调中的动画。另一个解决方案是在调用setState()之前检查此对象的“ mount”属性,以确保该对象仍在树中。 如果正在调用setState(),则此错误可能表示内存泄漏,因为从树中删除该状态对象后,另一个对象仍保留对该State对象的引用。为了避免内存泄漏,请考虑在dispose()期间中断对该对象的引用。)
随后的进一步调查显示,该应用程序占用了手机约400MB的内存(Ram),我发现这太高了。
帮助弄清楚这个问题确实会有所帮助。预先感谢。
其他信息: 在android 7.0(flutter 1.20.2)上运行时发生错误。请参见https://github.com/flutter/flutter/issues/35900上的类似/相关问题。请注意,我已升级到Flutter 1.20.2 adnd,降级到1.7.5将需要在升级后进行很多更改,尤其是在Firestore上(注意:https://firebase.flutter.dev/docs/migration,最近已更新)。
答案 0 :(得分:4)
await
之后,您的窗口小部件可能不再安装。那时执行setState会给您一个例外。这实际上是一件好事,因为您在其他地方,因此后面的代码无论如何都不应执行。
关于“在dispose()之后调用setState()”异常,您有三种选择:
if (!mounted) return;
和await
之间放置一个setState()
。将其放在每个await
之后可能是一个好习惯。setState()
呼叫替换为setStateIfMounted()
,并将其定义为:void setStateIfMounted(f) {
if (mounted) setState(f);
}
我在此video中解释了第三种方法。
答案 1 :(得分:3)
您可以使用:
if (this.mounted) { // check whether the state object is in tree
setState(() {
// make changes here
});
}
mounted
检查此State
对象当前是否在tree
中。
mounted class