坏状态:元素过多

时间:2020-10-14 13:31:54

标签: android flutter dart widget key

我收到此错误。我想要一个提示或摆脱该错误的方法。

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)

我已经创建了自定义小部件

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

在此小部件内,我有以下内容

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

我需要找到如上所述的特定TextFormField,但遇到上述错误。难道我做错了什么?我使用的密钥是唯一的,在整个代码中从未使用过。

我通过执行以下操作得到此错误:

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

,然后执行以下方法:

  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.

您能帮助我解决此问题吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

此问题是由于重复键造成的,在这种情况下,请尝试将键值传递给正确的小部件并为其创建键。

之前:

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

...

class MyCustonFormField{
  final Key key;

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

之后:

MyCustonFormField(
  key: valueKey,
  ...
)

...

class MyCustonFormField{
  dynamic valueKey;

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

答案 1 :(得分:1)

这都是关于重复键的。如果某些小部件具有相同的键,则会出现太多元素错误..