Skip to content
New issue

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

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

Already on GitHub? # to your account

flutter 3.10.1报错 Binding has not yet been initialized #14

Open
yujie-cheng opened this issue May 29, 2023 · 2 comments
Open

flutter 3.10.1报错 Binding has not yet been initialized #14

yujie-cheng opened this issue May 29, 2023 · 2 comments

Comments

@yujie-cheng
Copy link

image
@YangLang116
Copy link

image

所有使用window的地方改成view,比如view.physicalSize改成view.physicalSize

bool _isInitialized = false;

FlutterView get view => ui.PlatformDispatcher.instance.implicitView!;

class AutoSizeWidgetsBinding extends WidgetsFlutterBinding {
  final Queue<PointerEvent> _pendingPointerEvents = Queue<PointerEvent>();

  static WidgetsBinding ensureInitialized({required double designWidth}) {
    AutoSizeUtil.setStandard(designWidth, isAutoTextSize: false);
    if (!_isInitialized) {
      AutoSizeWidgetsBinding();
      _isInitialized = true;
    }
    return WidgetsBinding.instance;
  }

  @override
  ViewConfiguration createViewConfiguration() {
    ViewConfiguration c = super.createViewConfiguration();
    if (c.size == Size.zero) return c;
    //调整显示分辨率
    return ViewConfiguration(
      size: AutoSizeUtil.getLogicScreenSize(),
      devicePixelRatio: AutoSizeUtil.getDevicePixelRatio(),
    );
  }

  @override
  void initInstances() {
    super.initInstances();
    //调整手势位置
    platformDispatcher.onPointerDataPacket = _handlePointerDataPacket;
  }

  @override
  void cancelPointer(int pointer) {
    if (_pendingPointerEvents.isEmpty && !locked) {
      scheduleMicrotask(_flushPointerEventQueue);
    }
    _pendingPointerEvents.addFirst(PointerCancelEvent(pointer: pointer));
  }

  @override
  void unlocked() {
    super.unlocked();
    _flushPointerEventQueue();
  }

  void _handlePointerDataPacket(ui.PointerDataPacket packet) {
    // We convert pointer data to logical pixels so that e.g. the touch slop can be
    // defined in a device-independent manner.
    try {
      _pendingPointerEvents.addAll(PointerEventConverter.expand(
        packet.data,
        (int viewId) => AutoSizeUtil.getDevicePixelRatio(),
      ));
      if (!locked) {
        _flushPointerEventQueue();
      }
    } catch (error, stack) {
      FlutterError.reportError(FlutterErrorDetails(
        exception: error,
        stack: stack,
        library: 'gestures library',
        context: ErrorDescription('while handling a pointer data packet'),
      ));
    }
  }

  void _flushPointerEventQueue() {
    assert(!locked);
    while (_pendingPointerEvents.isNotEmpty) {
      handlePointerEvent(_pendingPointerEvents.removeFirst());
    }
  }
}

@auugkuu17
Copy link

FlutterError (No MediaQuery widget ancestor found.
ScaffoldMessenger widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was:
ScaffoldMessenger
The ownership chain for the affected widget is: "ScaffoldMessenger ← Builder ← DefaultTextStyle ← CustomPaint ← Banner ← CheckedModeBanner ← Title ← Directionality ← _LocalizationsScope-[GlobalKey#55012] ← Semantics ← ⋯"
No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because the context used is not a descendant of a View widget, which introduces a MediaQuery.)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants