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

Example code does not work: No MediaQuery widget ancestor found. #4

Closed
LostInDarkMath opened this issue Mar 11, 2022 · 1 comment
Closed

Comments

@LostInDarkMath
Copy link

Logs

Performing hot restart...
Syncing files to device Mi A2 Lite...
Restarted application in 2.508ms.

======== Exception caught by widgets library =======================================================
The following assertion was thrown building PersistentKeyboardHeightProvider(dirty, state: _PersistentKeyboardHeightProviderState#2ebf7):
No MediaQuery widget ancestor found.

PersistentKeyboardHeightProvider widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was: PersistentKeyboardHeightProvider
  dirty
  state: _PersistentKeyboardHeightProviderState#2ebf7
The ownership chain for the affected widget is: "PersistentKeyboardHeightProvider ← MyApp ← [root]"

No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.

The relevant error-causing widget was: 
  PersistentKeyboardHeightProvider PersistentKeyboardHeightProvider:file:///E:/Projekte/untitled2/lib/main.dart:14:18
When the exception was thrown, this was the stack: 
#0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:230:7)
#1      debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:245:4)
#2      MediaQuery.of (package:flutter/src/widgets/media_query.dart:859:12)
#3      _PersistentKeyboardHeightProviderState.build (package:flutter_persistent_keyboard_height/src/persistent_keyboard_height_provider.dart:104:32)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)
#9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4919:11)
#10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4729:5)
...     Normal element mounting (7 frames)
#17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3790:14)
#18     Element.updateChild (package:flutter/src/widgets/framework.dart:3540:18)
#19     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16)
#20     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5)
#21     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1112:18)
#22     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2600:19)
#23     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)
#24     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)
#25     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:924:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
====================================================================================================

======== Exception caught by widgets library =======================================================
The following assertion was thrown building PersistentKeyboardHeightProvider(dirty, state: _PersistentKeyboardHeightProviderState#2ebf7):
No MediaQuery widget ancestor found.

PersistentKeyboardHeightProvider widgets require a MediaQuery widget ancestor.
The specific widget that could not find a MediaQuery ancestor was: PersistentKeyboardHeightProvider
  dirty
  state: _PersistentKeyboardHeightProviderState#2ebf7
The ownership chain for the affected widget is: "PersistentKeyboardHeightProvider ← MyApp ← [root]"

No MediaQuery ancestor could be found starting from the context that was passed to MediaQuery.of(). This can happen because you have not added a WidgetsApp, CupertinoApp, or MaterialApp widget (those widgets introduce a MediaQuery), or it can happen if the context you use comes from a widget above those widgets.

The relevant error-causing widget was: 
  PersistentKeyboardHeightProvider PersistentKeyboardHeightProvider:file:///E:/Projekte/untitled2/lib/main.dart:14:18
When the exception was thrown, this was the stack: 
#0      debugCheckHasMediaQuery.<anonymous closure> (package:flutter/src/widgets/debug.dart:230:7)
#1      debugCheckHasMediaQuery (package:flutter/src/widgets/debug.dart:245:4)
#2      MediaQuery.of (package:flutter/src/widgets/media_query.dart:859:12)
#3      _PersistentKeyboardHeightProviderState.build (package:flutter_persistent_keyboard_height/src/persistent_keyboard_height_provider.dart:104:32)
#4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4870:27)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4928:11)
#7      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#8      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2659:19)
#9      WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)
#10     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#11     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#12     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#13     SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:862:7)
(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)
====================================================================================================
I/flutter ( 8532): INFO: PeriodicBackgroundTask: 2022-03-11 22:19:30.501332: PeriodicBackgroundService crash_reporter
I/flutter ( 8532): FINE: Controller: 2022-03-11 22:19:30.502671: sendCrashReport

Code

The code is exactly your example:

import 'package:flutter/material.dart';

import 'package:flutter_persistent_keyboard_height/flutter_persistent_keyboard_height.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const PersistentKeyboardHeightProvider(
      child: MaterialApp(
        title: 'Flutter Persistent Keyboard Height Example',
        home: FlutterPersistentKeyboardHeightExample(),
      ),
    );
  }
}

class FlutterPersistentKeyboardHeightExample extends StatelessWidget {
  const FlutterPersistentKeyboardHeightExample({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final keyboardHeight = PersistentKeyboardHeight.of(context).keyboardHeight;

    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Padding(
            padding: EdgeInsets.all(16.0),
            child: TextField(
              decoration: InputDecoration(
                labelText: 'Flutter Persistent Keyboard Size Example',
              ),
            ),
          ),
          const SizedBox(height: 8),
          Text('Keyboard height: $keyboardHeight'),
        ],
      ),
    );
  }
}

I use Flutter 2.10.

@f-person
Copy link
Owner

Thank you for the report! It's fixed now :)

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

2 participants