'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.



Solution 1:[1]

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.

Solution 2:[2]

Is your widget a Stateless widget by any chance?

TextEditingControllers need to be in a Stateful widget.

I was having the same issue when I tried converting my widget from Stateful to Stateless. Then I started seeing some weird errors from my TextEditingControllers so I converted it back to Stateful. Now it's fine.

So if you are using a Stateless Widget, try converting to Stateful and doing a full restart.

Solution 3:[3]

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"]),
                      ),
                    );
                  },
                );
              }
            },
          ),
        ),
     );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3