简体   繁体   English

在 state class 之外设置变量导致 flutter 应用程序滞后和崩溃

[英]Setting variables outside state class causing flutter app to lag and crash

I was a developing a flutter app when I suddenly came across an error that I simply can't solve.当我突然遇到一个我根本无法解决的错误时,我正在开发一个 flutter 应用程序。 I have a few variables declared in between the two classes of a stateful widget, and when I use setState() to update their values from within the <state> class, the app lags massively, and when more UI elements are added, the app will crash.我在有状态小部件的两个类之间声明了一些变量,当我使用setState()<state> class 更新它们的值时,应用程序会大量滞后,并且当添加更多 UI 元素时,应用程序会崩溃。 I have never run into this issue before, and I have used the same principle to make it easier to access variables outside of the file.我以前从未遇到过这个问题,并且我使用相同的原理来使访问文件外部的变量变得更加容易。

This has happened on Flutter 3.6 beta, 3.3.8, and 3.0.0, meaning it's not a Flutter version issue.这发生在 Flutter 3.6 beta、3.3.8 和 3.0.0 上,这意味着它不是 Flutter 版本问题。 It doesn't matter whether the variables are null or not, it doesn't matter whether the variables are called outside of initState or after a delay.变量是否为 null 无关紧要,变量是否在initState之外或在延迟之后调用也无关紧要。 The app loads this page directly from main.dart , meaning it's also not because of any prior discrepancy.该应用程序直接从main.dart加载此页面,这意味着它也不是因为任何先前的差异。

I have reduced the error into a single file.我已将错误减少到一个文件中。 Create a new project, copy the below code, and replace with the existing code in main.dart .新建一个项目,复制下面的代码,并替换为main.dart中已有的代码。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const ErrorTest(),
    );
  }
}

class ErrorTest extends StatefulWidget {
  const ErrorTest({super.key});

  @override
  State<ErrorTest> createState() => _ErrorTestState();
}

int? points;
String? firstName;
String? lastName;
String? email;
String? profilePicture;
int? timestamp;
String? date;
String? time;
String? institution;
String? address;

class _ErrorTestState extends State<ErrorTest> {
  @override
  void initState() {
    setState(() {
      firstName = "johny";
      lastName = "johny";
      email = "eeaeaa";
      profilePicture = "a url";
      timestamp = 123456789;
      date = "2021-09-09";
      time = "09:09:09";
      institution = "123456789";
      address = "123456789";
      points = 123456789;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Text(
      firstName.toString(),
    );
  }
}

Error log错误记录

Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
√  Built build\app\outputs\flutter-apk\app-debug.apk.
Connecting to VM Service at ws://127.0.0.1:54124/Kq8WOPDGOY8=/ws
I/scudo   ( 5917): Stats: SizeClassAllocator64: 266M mapped (0M rss) in 322652 allocations; remains 269165
I/scudo   ( 5917):   00 (    64): mapped:    256K popped:    2171 pushed:    1950 inuse:    221 total:    728 rss:      0K releases:      0 last released:      0K region: 0x7dc6b6ce2000 (0x7dc6b6cd7000)
I/scudo   ( 5917):   01 (    32): mapped:    256K popped:    4498 pushed:    1950 inuse:   2548 total:   2548 rss:      0K releases:      0 last released:      0K region: 0x7dc6c6ce3000 (0x7dc6c6cd7000)
I/scudo   ( 5917):   02 (    48): mapped:    512K popped:   16055 pushed:    8268 inuse:   7787 total:   7800 rss:      0K releases:      0 last released:      0K region: 0x7dc6d6ce7000 (0x7dc6d6cd7000)
I/scudo   ( 5917):   03 (    64): mapped:    512K popped:    8412 pushed:    2132 inuse:   6280 total:   6332 rss:      0K releases:      0 last released:      0K region: 0x7dc6e6cdf000 (0x7dc6e6cd7000)
I/scudo   ( 5917):   04 (    80): mapped:    256K popped:    4771 pushed:    2444 inuse:   2327 total:   2444 rss:      0K releases:      0 last released:      0K region: 0x7dc6f6ce1000 (0x7dc6f6cd7000)
I/scudo   ( 5917):   05 (    96): mapped:    256K popped:    1079 pushed:     286 inuse:    793 total:    832 rss:      0K releases:      0 last released:      0K region: 0x7dc706ce7000 (0x7dc706cd7000)
I/scudo   ( 5917):   06 (   112): mapped:    256K popped:    1625 pushed:    1313 inuse:    312 total:    728 rss:      0K releases:      0 last released:      0K region: 0x7dc716ce2000 (0x7dc716cd7000)
I/scudo   ( 5917):   07 (   144): mapped:    256K popped:    1989 pushed:     650 inuse:   1339 total:   1352 rss:      0K releases:      0 last released:      0K region: 0x7dc726cdd000 (0x7dc726cd7000)
I/scudo   ( 5917):   08 (   176): mapped:    512K popped:    2711 pushed:    1443 inuse:   1268 total:   1853 rss:      0K releases:      0 last released:      0K region: 0x7dc736ce0000 (0x7dc736cd7000)
I/scudo   ( 5917):   09 (   192): mapped:    256K popped:     169 pushed:      13 inuse:    156 total:    156 rss:      0K releases:      0 last released:      0K region: 0x7dc746cdf000 (0x7dc746cd7000)
I/scudo   ( 5917):   10 (   224): mapped:    512K popped:    1365 pushed:      26 inuse:   1339 total:   1378 rss:      0K releases:      0 last released:      0K region: 0x7dc756ce6000 (0x7dc756cd7000)
I/scudo   ( 5917):   11 (   288): mapped:    256K popped:     650 pushed:      26 inuse:    624 total:    624 rss:      0K releases:      0 last released:      0K region: 0x7dc766ce2000 (0x7dc766cd7000)
I/scudo   ( 5917):   12 (   352): mapped:    256K popped:    1105 pushed:     949 inuse:    156 total:    728 rss:      0K releases:      6 last released:    124K region: 0x7dc776ce0000 (0x7dc776cd7000)
I/scudo   ( 5917):   13 (   448): mapped:    256K popped:     364 pushed:      91 inuse:    273 total:    312 rss:      0K releases:      1 last released:      4K region: 0x7dc786cdd000 (0x7dc786cd7000)
I/scudo   ( 5917):   14 (   592): mapped:    256K popped:     507 pushed:     247 inuse:    260 total:    416 rss:      0K releases:      4 last released:      8K region: 0x7dc796cd8000 (0x7dc796cd7000)
I/scudo   ( 5917):   15 (   800): mapped:    256K popped:      90 pushed:       0 inuse:     90 total:    120 rss:      0K releases:      0 last released:      0K region: 0x7dc7a6cde000 (0x7dc7a6cd7000)
I/scudo   ( 5917): F 16 (  1104): mapped: 261888K popped:  274165 pushed:   31255 inuse: 242910 total: 242910 rss:      0K releases:     31 last released:   3396K region: 0x7dc7b6cd9000 (0x7dc7b6cd7000)
I/scudo   ( 5917):   17 (  1648): mapped:    256K popped:      60 pushed:       4 inuse:     56 total:     64 rss:      0K releases:      0 last released:      0K region: 0x7dc7c6ce7000 (0x7dc7c6cd7000)
I/scudo   ( 5917):   18 (  2096): mapped:    256K popped:      78 pushed:      24 inuse:     54 total:     60 rss:      0K releases:      2 last released:      8K region: 0x7dc7d6ce1000 (0x7dc7d6cd7000)
I/scudo   ( 5917):   19 (  2576): mapped:    256K popped:      96 pushed:       0 inuse:     96 total:     96 rss:      0K releases:      0 last released:      0K region: 0x7dc7e6cde000 (0x7dc7e6cd7000)
I/scudo   ( 5917):   20 (  3120): mapped:    256K popped:      36 pushed:       2 inuse:     34 total:     40 rss:      0K releases:      1 last released:      8K region: 0x7dc7f6ce3000 (0x7dc7f6cd7000)
I/scudo   ( 5917):   21 (  4112): mapped:    512K popped:     224 pushed:     161 inuse:     63 total:     67 rss:      0K releases:      4 last released:      4K region: 0x7dc806cdf000 (0x7dc806cd7000)
I/scudo   ( 5917):   22 (  4624): mapped:    256K popped:       5 pushed:       2 inuse:      3 total:      8 rss:      0K releases:      2 last released:     20K region: 0x7dc816ce3000 (0x7dc816cd7000)
I/scudo   ( 5917):   23 (  7120): mapped:    512K popped:      99 pushed:      48 inuse:     51 total:     56 rss:      0K releases:     32 last released:     24K region: 0x7dc826ce3000 (0x7dc826cd7000)
I/scudo   ( 5917):   24 (  8720): mapped:    256K popped:      27 pushed:       0 inuse:     27 total:     28 rss:      0K releases:      0 last released:      0K region: 0x7dc836cdc000 (0x7dc836cd7000)
I/scudo   ( 5917):   25 ( 11664): mapped:    256K popped:      22 pushed:       2 inuse:     20 total:     22 rss:      0K releases:      1 last released:      8K region: 0x7dc846cdd000 (0x7dc846cd7000)
I/scudo   ( 5917):   26 ( 14224): mapped:    512K popped:      24 pushed:       2 inuse:     22 total:     26 rss:      0K releases:      1 last released:     12K region: 0x7dc856ce7000 (0x7dc856cd7000)
I/scudo   ( 5917):   27 ( 16400): mapped:    256K popped:      16 pushed:       1 inuse:     15 total:     15 rss:      0K releases:      2 last released:     28K region: 0x7dc866cdc000 (0x7dc866cd7000)
I/scudo   ( 5917):   28 ( 18448): mapped:    256K popped:       5 pushed:       0 inuse:      5 total:      8 rss:      0K releases:      0 last released:      0K region: 0x7dc876cde000 (0x7dc876cd7000)
I/scudo   ( 5917):   29 ( 23056): mapped:    256K popped:       9 pushed:       1 inuse:      8 total:      8 rss:      0K releases:      3 last released:     84K region: 0x7dc886ce0000 (0x7dc886cd7000)
I/scudo   ( 5917):   30 ( 29456): mapped:    512K popped:      13 pushed:       4 inuse:      9 total:     12 rss:      0K releases:      2 last released:     24K region: 0x7dc896ce4000 (0x7dc896cd7000)
I/scudo   ( 5917):   31 ( 33296): mapped:    256K popped:     197 pushed:     192 inuse:      5 total:      7 rss:      0K releases:      5 last released:     28K region: 0x7dc8a6cdd000 (0x7dc8a6cd7000)
I/scudo   ( 5917):   32 ( 65552): mapped:   1024K popped:      15 pushed:       1 inuse:     14 total:     15 rss:      0K releases:      1 last released:     60K region: 0x7dc8b6ce5000 (0x7dc8b6cd7000)
I/scudo   ( 5917): Scudo OOM: The process has exhausted 256M for size class 1104.
E/SurfaceSyncer( 5917): Failed to find sync for id=0
W/Parcel  ( 5917): Expecting binder but got null!
Reloaded 1 of 594 libraries in 597ms (compile: 55 ms, reload: 129 ms, reassemble: 155 ms).
D/EGL_emulation( 5917): app_time_stats: avg=8695.38ms min=53.84ms max=25809.32ms count=3
Reloaded 1 of 594 libraries in 2,177ms (compile: 18 ms, reload: 126 ms, reassemble: 1910 ms).
D/EGL_emulation( 5917): app_time_stats: avg=18633.52ms min=18633.52ms max=18633.52ms count=1
Lost connection to device.
Exited (sigterm)

Why are you declaring your variables outside of all classes?为什么要在所有类之外声明变量? You can declare them inside your state class with no problem.您可以在 state class 中声明它们,没有问题。

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const ErrorTest(),
    );
  }
}

class ErrorTest extends StatefulWidget {
  const ErrorTest({super.key});

  @override
  State<ErrorTest> createState() => _ErrorTestState();
}

class _ErrorTestState extends State<ErrorTest> {
  int? points;
  String? firstName;
  String? lastName;
  String? email;
  String? profilePicture;
  int? timestamp;
  String? date;
  String? time;
  String? institution;
  String? address;
  @override
  void initState() {
    setState(() {
      firstName = "johny";
      lastName = "johny";
      email = "eeaeaa";
      profilePicture = "a url";
      timestamp = 123456789;
      date = "2021-09-09";
      time = "09:09:09";
      institution = "123456789";
      address = "123456789";
      points = 123456789;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          firstName.toString(),
        ),
      ),
    );
  }
}

After a week of looking for a solution, I found that the issue is that I should have gone even a step lower and install Flutter 2.10.5.在寻找解决方案一周后,我发现问题在于我应该更进一步并安装 Flutter 2.10.5。 Reinstalling and resetting the path eliminates this issue.重新安装并重置路径可消除此问题。 Will have to wait for flutter to fix this in future versions.以后的版本要等flutter来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM