错误在两个区域抛出(并且应用程序冻结(当应用程序最小化时,当单击手机后退按钮时,或者当另一个应用程序在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');
 });
}

}

来自框架的错误.dart go 巴州.

主页上的getItems()也出现了同样的错误

出现异常.

进一步的调查显示,这款应用程序占用了大约400MB的内存(RAM),我觉得这个数字太高了.

帮助解决这个问题真的会很有帮助.提前谢谢您.

其他信息: 在Android 7.0,Ffltter 1.20.2上运行时出错.请参阅https://github.com/flutter/flutter/issues/35900上的类似/相关问题.请注意,我升级到了Flutter 1.20.2,而降级到1.7.5需要我在升级后做很多更改,特别是在Firestore上(注:https://firebase.flutter.dev/docs/migration,最近更新了).

推荐答案

await之后,您的小部件可能不再被挂载.执行setState时会给您带来一个异常.这实际上是件好事,后面的代码无论如何都不应该执行,因为您在其他地方.

关于"Dispose()之后调用的setState()"异常,您有三种 Select :

  1. 安全地忽略它.例外情况是保存异步函数使其不能继续.
  2. awaitsetState()之间各加一个if (!mounted) return;.把它放在每个await后面可能是个好习惯.
  3. 将您的setState()个呼叫替换为setStateIfMounted(),并将其定义为:
void setStateIfMounted(f) {
  if (mounted) setState(f);
}

我在这video篇文章中解释了这些方法.

Dart相关问答推荐

如何防止 Riverpod 的 ConsumerWidget 重建管理 ThemeMode

Asset图像作为Card背景,Flutter 中顶部有文本

AngularDart 可以直接路由到组件吗?

flutter pub run build_runner 卡住了

Angular 2,使用 body 作为根 Select 器,而不是 my-app

如何在Flutter应用程序中使用套接字?

属性 'docs' 不能无条件访问,因为接收到的可能是 'null' Flutter

Flutter插件和Flutter模块之间有什么区别?

dart:web_gl: 渲染警告:texture bound to texture unit 0 is not renderable

如何在imageprovider类型中传递图像资源?

如何在Dart中处理JSON

如何在 flutter 中对json进行字符串化

在 Dart 中发送 SMTP 邮箱

在 Dart 中调用异步函数而不等待,例如启动一个线程

如何在 Dart 中创建我们自己的metadata元数据?

VS Code 首选项的位置

如何在 Ubuntu 网络服务器上为 Dart 安装 pub(命令行使用)

包裹构造函数参数的花括号代表什么?

在 Dart 中将类型化函数作为参数传递

如何在 Dart 中将 double 转换为 int?