切换选项卡时间歇性滞后

发布于 2025-02-07 21:41:57 字数 1735 浏览 3 评论 0原文

这是我的第一个扑朔迷离的项目,我正在学习。我最初在我的主体中都有所有10,000行代码,而令人惊讶的是,切换选项卡时我没有遇到滞后。从那以后,我根据屏幕,类和功能将其分解为文件,并且在切换选项卡时滞后,但仅偶尔 - 似乎是随机的。

起初,我在Globals.dart文件中有valueNotifier。我认为这可能是问题,所以我在某些地方实施了一个提供商(尚未到处这样做,因为我认为它没有解决问题)。

我有一个显示瓷砖的列表页面。这是一些代码:

class ListPage extends StatefulWidget {
  const ListPage({Key? key}) : super(key: key);
  @override
  _ListState createState() => _ListState();
}

class _ListState extends State<ListPage> {
  @override
  Widget build(BuildContext context) {
    context.watch<ListProvider>().getIndivWorkouts();
    var listProvider = Provider.of<ListProvider>(context);
    return Scaffold(
        body: ListView(
                children: <Widget>[
                    for (int index = listProvider.indivWorkouts.length - 1;
                          index >= 0; index--)
                    Column(children: <Widget>[// just builds tile]
...

提供商:

class ListProvider with ChangeNotifier {
  List _indivWorkouts = <IndivWorkout>[];
  List get indivWorkouts => _indivWorkouts.toList();

  getIndivWorkouts() {
    final box = Hive.box<IndivWorkout>('indivWorkoutsBox');
    _indivWorkouts = box.values.toList();
  }

  addIndivWorkout(IndivWorkout workout) {
    var indivWorkoutsBox = Hive.box<IndivWorkout>('indivWorkoutsBox');
    indivWorkoutsBox.add(workout);
    notifyListeners();
  }
}

AddIndivWorkout从另一个页面调用。

我试图筛选该问题,但是在最后的录制中看起来完全正常,所以我不得不使用另一台设备拍摄。注意从击中标签到页面加载的延迟。

https://i.sstatic.net/8npeq.jpg 释放比调试构建奇怪。此外,即使我明显看到大延迟,DevTools性能图也似乎正常。我真的不想回到一个巨大的主要文件。

This is my first Flutter project, and I'm learning as I go. I originally had all 10,000 lines of code in my main.dart, and surprisingly I was not running into lag when switching tabs. I've since broken it up into files based on screens, classes, and functions, and I'm lag when switching tabs, but only occasionally—it seems random.

At first, I had ValueNotifiers in a globals.dart file. I thought that might be the issue, so I implemented a provider in some places (haven't done it everywhere yet because I don't think it is fixing the issue).

I have a list page that displays tiles. Here is some code:

class ListPage extends StatefulWidget {
  const ListPage({Key? key}) : super(key: key);
  @override
  _ListState createState() => _ListState();
}

class _ListState extends State<ListPage> {
  @override
  Widget build(BuildContext context) {
    context.watch<ListProvider>().getIndivWorkouts();
    var listProvider = Provider.of<ListProvider>(context);
    return Scaffold(
        body: ListView(
                children: <Widget>[
                    for (int index = listProvider.indivWorkouts.length - 1;
                          index >= 0; index--)
                    Column(children: <Widget>[// just builds tile]
...

And the provider:

class ListProvider with ChangeNotifier {
  List _indivWorkouts = <IndivWorkout>[];
  List get indivWorkouts => _indivWorkouts.toList();

  getIndivWorkouts() {
    final box = Hive.box<IndivWorkout>('indivWorkoutsBox');
    _indivWorkouts = box.values.toList();
  }

  addIndivWorkout(IndivWorkout workout) {
    var indivWorkoutsBox = Hive.box<IndivWorkout>('indivWorkoutsBox');
    indivWorkoutsBox.add(workout);
    notifyListeners();
  }
}

addIndivWorkout is called from another page.

I tried to screen record the issue, but it appeared perfectly normal in the final recording, so I had to film with another device. Notice the delay from hitting the tab to the page loading.

https://i.sstatic.net/8NPeQ.jpg

The delay seems more frequent on the release than the debug build, strangely. Additionally, even when I am visibly seeing a large delay, the DevTools performance graph appears normal. I really don't want to go back to one huge main.dart file, so any help is appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迟到的我 2025-02-14 21:41:57

我将检查消费类:

https://pub.dev/ documentation/provider/provider/provider/consumer-class.html

https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple

一个一个示例,说明了如何拥有代码。

我相信,这可能有助于滞后,

@override
 Widget build(BuildContext context) {
   return Scaffold(
        body: Consumer<ListProvider>(
                builder: (context, listProvider, child) { 
                  return ListView(
                    children: <Widget>[
                    for (int index = listProvider.indivWorkouts.length - 1;
                          index >= 0; index--)
                    Column(children: <Widget>[// just builds tile]
                },
              );
 }

请确保注意第二个链接,并将您的消费者尽可能地放入小部件树中。因为“您不想仅仅因为某个地方发生了一些细节而重建UI的大部分。”

I would check out the Consumer class:

https://pub.dev/documentation/provider/latest/provider/Consumer-class.html

https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple

A little example of how you could have your code.

I believe that this may help with the lagging

@override
 Widget build(BuildContext context) {
   return Scaffold(
        body: Consumer<ListProvider>(
                builder: (context, listProvider, child) { 
                  return ListView(
                    children: <Widget>[
                    for (int index = listProvider.indivWorkouts.length - 1;
                          index >= 0; index--)
                    Column(children: <Widget>[// just builds tile]
                },
              );
 }

Make sure to take note of the second link and put your consumer as far down in the widget tree as possible. Because "You don’t want to rebuild large portions of the UI just because some detail somewhere changed."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文