lordarcadius
lordarcadius

Reputation: 231

Getting "Assertion failed:_pressedKeys.containsKey(event.physicalKey)" error in Flutter Web

I fetched the data from an API through POST request. Then I populated the data inside a GridView Builder. But when I am scrolling the web version of the app it is giving me this error:

════════ Exception caught by services library ══════════════════════════════════
The following assertion was thrown during a platform message callback:
Assertion failed:
../…/services/hardware_keyboard.dart:441
_pressedKeys.containsKey(event.physicalKey)
"A KeyUpEvent is dispatched, but the state shows that the physical key is not pressed. If this occurs in real application, please report this bug to Flutter. If this occurs in unit tests, please ensure that simulated events follow Flutter's event model as documented in `HardwareKeyboard`. This was the event: KeyUpEvent#6f053(physicalKey: PhysicalKeyboardKey#700e3(usbHidUsage: \"0x000700e3\", debugName: \"Meta Left\"), logicalKey: LogicalKeyboardKey#4b191(keyId: \"0x200000106\", keyLabel: \"Meta Left\", debugName: \"Meta Left\"), character: null, timeStamp: 0:01:32.201000, synthesized)"


When the exception was thrown, this was the stack
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 251:49  throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3    assertFailed
packages/flutter/src/services/hardware_keyboard.dart 441:46                   <fn>
packages/flutter/src/services/hardware_keyboard.dart 451:14                   [_assertEventIsRegular]
packages/flutter/src/services/hardware_keyboard.dart 543:5                    handleKeyEvent
packages/flutter/src/services/hardware_keyboard.dart 821:57                   handleRawKeyMessage
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54            runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5            _async
packages/flutter/src/services/hardware_keyboard.dart 808:51                   handleRawKeyMessage
packages/flutter/src/services/platform_channel.dart 73:49                     <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54            runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5            _async
packages/flutter/src/services/platform_channel.dart 72:58                     <fn>
packages/flutter/src/services/binding.dart 379:35                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 84:54            runBody
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 123:5            _async
packages/flutter/src/services/binding.dart 376:98                             <fn>
lib/_engine/engine/platform_dispatcher.dart 1034:13                           invoke2
lib/ui/src/ui/channel_buffers.dart 25:5                                       invoke
lib/ui/src/ui/channel_buffers.dart 65:7                                       push
lib/ui/src/ui/channel_buffers.dart 130:16                                     push
lib/_engine/engine/platform_dispatcher.dart 302:25                            invokeOnPlatformMessage
lib/_engine/engine/keyboard.dart 130:39                                       [_handleHtmlEvent]
lib/_engine/engine/keyboard.dart 39:7                                         <fn>
════════════════════════════════════════════════════════════════════════════════

This is the code for populating the GridView. There isn't any error or issue with GridView. The data is being populated perfectly but as soon as I start scrolling, the above error shows in Debug console. Also, the scrolling is very laggy. It seems as if the animations are completely broken.

@override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
          body: Row(
        children: [
          SideDrawer(),
          Container(
            padding: EdgeInsets.all(20),
            width: MediaQuery.of(context).size.width * 0.77,
            child: FutureBuilder<dynamic>(
              future: appList(),
              builder: (context, snapshot) {
                if (snapshot.data == null) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                } else {
                  return GridView.builder(
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 5,
                        childAspectRatio: 3 / 2,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10),
                    itemCount: snapshot.data.length - 1,
                    itemBuilder: (context, index) {
                      return Card(
                        elevation: 2,
                        child: ListTile(
                          leading: Text(snapshot.data[index]["title"]),
                        ),
                      );
                    },
                  );
                }
              },
            ),
          ),
        ],
      )),
    );
  }

Any help will be appreciable. Thank you so much.

Upvotes: 22

Views: 21448

Answers (8)

1housand
1housand

Reputation: 606

If you are using a StatelessWidget, convert it to a StatefulWidget and do a full restart.

In the code example from the official Flutter API documentation for TextEditingController, they are using a StatefulWidget so it's safe to assume that TextEditingControllers need to be in a StatefulWidget.

Upvotes: 5

blackbird
blackbird

Reputation: 500

I had a similar issue while developing flutter for desktop apps, I was getting the error due to having capslock on, ensure it is off, for somewierd reason, on an M2 chip Macbook, and you run the app on debug mode with capslock on, you get that error. Run the app with the caps off

Upvotes: 0

Jack&#39;
Jack&#39;

Reputation: 2498

I had the same problem, and even reinstalling the app didn't work.

The problem was with my simulator (iPad). So I :

  • Exited my simulator
  • Launched the simulator again
  • Reinstalled the app

And the problem disappeared.

Upvotes: 0

Harsh Kothari
Harsh Kothari

Reputation: 512

This error also occurs when you use virtual emulators connected to your real device and you try to use physical keyboard as an input instead of Real device's keyboard

so try to input through the real device's input mode

Upvotes: 0

Md omer arafat
Md omer arafat

Reputation: 466

you will not find this error in release mode, this is caused by the Emulator it self. You can ignore it!

Upvotes: 0

Sudhanshu Namdev
Sudhanshu Namdev

Reputation: 21

When you use a physical keyboard on an emulator rapidly, this error occurred. Use 'Hot Restart' or use Emulators/Simulators keyboard while testing the app.

Upvotes: 2

Gregory Seront
Gregory Seront

Reputation: 836

This is an issue with hot reload: https://github.com/flutter/flutter/issues/87391. I experienced the same problem, but once I stop/relaunch the app, it disappears.

Upvotes: 21

Jahidul Islam
Jahidul Islam

Reputation: 12575

Add this two lines shrinkWrap: true and physics: ScrollPhysics(),under GridView.builder

return Scaffold(
      drawer: SideDrawer(),
        body: Container(
          padding: EdgeInsets.all(20),
          width: MediaQuery.of(context).size.width * 0.77,
          child: FutureBuilder<dynamic>(
            future: appList(),
            builder: (context, snapshot) {
              if (snapshot.data == null) {
                return Center(
                  child: CircularProgressIndicator(),
                );
              } else {
                return GridView.builder(
                  shrinkWrap: true,
                  physics: ScrollPhysics(),
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 5,
                      childAspectRatio: 3 / 2,
                      crossAxisSpacing: 10,
                      mainAxisSpacing: 10),
                  itemCount: snapshot.data.length - 1,
                  itemBuilder: (context, index) {
                    return Card(
                      elevation: 2,
                      child: ListTile(
                        leading: Text(snapshot.data[index]["title"]),
                      ),
                    );
                  },
                );
              }
            },
          ),
        ),
     );

Upvotes: 1

Related Questions