dart-lang / dart-pad

An online Dart editor with support for console, web, and Flutter apps
https://dartpad.dev
BSD 3-Clause "New" or "Revised" License
1.7k stars 554 forks source link

[error] A message on the flutter/lifecycle channel was discarded before it could be handled. #3046

Open iapicca opened 1 month ago

iapicca commented 1 month ago

What happened?

this code sample works locally both with flutter run -d chrome / flutter run -d chrome --release and served with dhttpd (see here), but fails on dartpad

Steps to reproduce problem

click on this link

Additional info

Browser

• Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.71

Are you using any extensions/plugins that affect website behavior (particularly those that affect iframes, such as ad blockers)?

NO

Are there any warnings or errors in your browser's JavaScript console? If so, paste them below:

Uncaught (in promise) Error: SecurityError: Failed to read the 'localStorage' property from 'Window': The document is sandboxed and lacks the 'allow-same-origin' flag.

    at Object.createErrorWithStack (errors.dart:329:10)
    at Error._throw (core_patch.dart:265:28)
    at Error.throwWithStackTrace (errors.dart:120:5)
    at async._AsyncCallbackEntry.new.callback (zone.dart:1386:11)
    at Object._microtaskLoop (schedule_microtask.dart:40:11)
    at _startMicrotaskLoop (schedule_microtask.dart:49:5)
    at async_patch.dart:181:7

Machine

                    'c.          francesco@Francescos-MacBook-Air.local
                 ,xNMM.          --------------------------------------
               .OMMMMo           OS: macOS 14.6.1 23G93 arm64
               OMMM0,            Host: MacBookAir10,1
     .;loddo:' loolloddol;.      Kernel: 23.6.0
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Uptime: 10 days, 23 mins
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    Packages: 180 (brew)
 XMMMMMMMMMMMMMMMMMMMMMMMX.      Shell: zsh 5.9
;MMMMMMMMMMMMMMMMMMMMMMMM:       Resolution: 1440x900
:MMMMMMMMMMMMMMMMMMMMMMMM:       DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Quartz Compositor
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.    WM Theme: Blue (Dark)
 .XMMMMMMMMMMMMMMMMMMMMMMMMMMk   Terminal: WarpTerminal
  .XMMMMMMMMMMMMMMMMMMMMMMMMK.   CPU: Apple M1
    kMMMMMMMMMMMMMMMMMMMMMMd     GPU: Apple M1
     ;KMMMMMMMWXXWMMMMMMMk.      Memory: 1507MiB / 8192MiB
       .cooc,.    .,coo:.

Your code

import 'package:flutter/foundation.dart' show ValueListenable;
import 'package:shared_preferences/shared_preferences.dart'
    show SharedPreferences;
import 'package:flutter/material.dart';

void main() async {
  final counter = await SharedCounter.instance();
  runApp(
    MaterialApp(
      home: Scaffold(
        body: Center(
          child: ValueListenableBuilder<int>(
            valueListenable: counter,
            builder: (context, count, _) => Text('$count'),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => counter.value++,
          tooltip: 'Increment',
          child: const Icon(Icons.add),
        ),
      ),
    ),
  );
}

abstract class Counter extends ValueListenable<int> with ChangeNotifier {
  @override
  int get value;
  set value(int value);
}

final class SharedCounter extends Counter {
  SharedCounter._(
    this._prefs,
    String key,
    this.initialValue,
  ) : _key = key;

  final SharedPreferences _prefs;
  final String _key;
  final int initialValue;

  static Future<SharedCounter> instance({
    String key = 'COUNTER',
    initialValue = 0,
  }) async =>
      SharedCounter._(
        await SharedPreferences.getInstance(),
        key,
        initialValue,
      );

  @override
  int get value => _prefs.getInt(_key) ?? initialValue;

  @override
  set value(int value) {
    _prefs.setInt(_key, value);
    notifyListeners();
  }
}

DartPad's output

A message on the flutter/lifecycle channel was discarded before it could be handled.
This happens when a plugin sends messages to the framework side before the framework has had an opportunity to register a listener. See the ChannelBuffers API documentation for details on how to configure the channel to expect more messages, or to expect messages to get discarded:
  https://api.flutter.dev/flutter/dart-ui/ChannelBuffers-class.html
polina-c commented 3 days ago

Other references to this kind of issue: https://github.com/flutter/flutter/issues/131452 https://github.com/flutter/flutter/issues/152522 https://github.com/flutter/flutter/issues/144155 https://github.com/flutter/flutter/issues/42880 https://stackoverflow.com/questions/78809004/flutter-web-only-shows-white-screen