Home > Software engineering >  Setting variables outside state class causing flutter app to lag and crash
Setting variables outside state class causing flutter app to lag and crash

Time:11-23

I was a developing a flutter app when I suddenly came across an error that I simply can't solve. 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. 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. 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. The app loads this page directly from main.dart, meaning it's also not because of any prior discrepancy.

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.

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)

CodePudding user response:

Why are you declaring your variables outside of all classes? You can declare them inside your state class with no problem.

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(),
        ),
      ),
    );
  }
}

CodePudding user response:

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. Reinstalling and resetting the path eliminates this issue. Will have to wait for flutter to fix this in future versions.

  • Related