Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
iapicca opened this issue Aug 25, 2024 · 1 comment

Comments

@iapicca
Copy link

iapicca commented Aug 25, 2024

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.          [email protected]
                 ,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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@polina-c @iapicca and others