簡體   English   中英

在 state class 之外設置變量導致 flutter 應用程序滯后和崩潰

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

當我突然遇到一個我根本無法解決的錯誤時,我正在開發一個 flutter 應用程序。 我在有狀態小部件的兩個類之間聲明了一些變量,當我使用setState()<state> class 更新它們的值時,應用程序會大量滯后,並且當添加更多 UI 元素時,應用程序會崩潰。 我以前從未遇到過這個問題,並且我使用相同的原理來使訪問文件外部的變量變得更加容易。

這發生在 Flutter 3.6 beta、3.3.8 和 3.0.0 上,這意味着它不是 Flutter 版本問題。 變量是否為 null 無關緊要,變量是否在initState之外或在延遲之后調用也無關緊要。 該應用程序直接從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(),
    );
  }
}

錯誤記錄

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)

為什么要在所有類之外聲明變量? 您可以在 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(),
        ),
      ),
    );
  }
}

在尋找解決方案一周后,我發現問題在於我應該更進一步並安裝 Flutter 2.10.5。 重新安裝並重置路徑可消除此問題。 以后的版本要等flutter來解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM