Bad state: Too many elements FLUTTER

4,920

Solution 1

This problem is due to duplicate keys, in this case try to pass the key value to the correct widget and create the key for it.

Before:

MyCustonFormField(
  key: Key(valueKey),
  ...
)

...

class MyCustonFormField{
  final Key key;

  ...
  Widget build(BuildContext context) {
  return TextField(
     key: this.key,
  )
}

After:

MyCustonFormField(
  key: valueKey,
  ...
)

...

class MyCustonFormField{
  dynamic valueKey;

  ...
  Widget build(BuildContext context) {
  return TextField(
     key: Key(this.valueKey),
  )
}

Solution 2

it's all about duplicate keys. If some widgets have same keys you get too many elements error..

Share:
4,920
Apuna12
Author by

Apuna12

Updated on December 24, 2022

Comments

  • Apuna12
    Apuna12 over 1 year

    I am getting this error. I would like a hint or something to get rid of this error.

    Unhandled exception:
    DriverError: Error in Flutter application: Uncaught extension error while executing tap: Bad state: Too many elements
    #0      Iterable.single (dart:core/iterable.dart:556:24)
    #1      WidgetController._getElementPoint (package:flutter_test/src/controller.dart:646:47)
    #2      WidgetController.getCenter (package:flutter_test/src/controller.dart:618:12)
    #3      WidgetController.tap (package:flutter_test/src/controller.dart:256:18)
    #4      FlutterDriverExtension._tap (package:flutter_driver/src/extension/extension.dart:388:19)
    <asynchronous suspension>
    #5      FlutterDriverExtension.call (package:flutter_driver/src/extension/extension.dart:216:53)
    #6      BindingBase.registerServiceExtension.<anonymous closure> (package:flutter/src/foundation/binding.dart:528:32)
    <asynchronous suspension>
    #7      BindingBase.registerServiceExtension.<anonymous closure> (package:flutter/src/foundation/binding.dart)
    #8      _runExtension (dart:developer-patch/developer.dart:86:23)
    
    

    I have created custom widget

    FrameInputField(
              key: ValueKey("login_email"),
              name: 'EMAIL',
              controller: _usernameController,
            ),
    

    inside of this widget I have the following

    class FrameInputField extends StatefulWidget {
      final TextEditingController controller;
      final String name;
    
      const FrameInputField(
          {@required this.controller,
          @required this.name,
          Key key}) : super(key:key);
    
      @override
      _FrameInputFieldState createState() => _FrameInputFieldState();
    }
    class _FrameInputFieldState extends State<FrameInputField> {
      bool _showPassword = false;
      bool lowercase = false;
      bool special = false;
      bool uppercase = false;
      bool eight = false;
      bool number = false;
    
     @override
      Widget build(BuildContext context) {
        return Container(
    Focus(
                onFocusChange: (hasFocus) {
                  if (!hasFocus)
                    setState(() {
                      _showPassword = false;
                    });
                },
                child: TextFormField(
                  validator: (value) {
                    if (widget.tries != null) {
                      return 'Incorrect password.';
                    } else
                      return null;
                  },
                  key: widget.key,
                  keyboardType: widget.numKeyboard ? TextInputType.number : null,
                  controller: widget.controller,
                  enabled: widget.enabled,
    .
    .
    .
    .
    
    

    I need to find that particular TextFormField as mentioned above, but I get the mentioned error. Am I doing something wrong? The key I am using is unique and never been used in the whole code.

    I get this error by doing the following:

    final emailTextField = find.byValueKey("login_email");
    

    and is followed by this method:

      Future<String> setEmail (String email) async {
        await _driver.tap(emailTextField);
        await _driver.enterText(email);
        assert(_driver.getText(passwordTextField).toString() == email);
      }
    

    `

    [√] Flutter (Channel stable, v1.17.4, on Microsoft Windows [Version 10.0.16299.1127], locale en-US)
    
    [√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    [√] Android Studio (version 4.0)
    [√] IntelliJ IDEA Community Edition (version 2018.2)
    [!] Connected device
        ! No devices available
    
    ! Doctor found issues in 1 category.
    

    Can you help me resolve this issue?

    Thanks!