添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

在部件树Flutter中检测到重复的GlobalKey

3 人关注

我有一个列表视图,它有一个卡片作为列表视图的一个子节点。该卡片包含一个带有表单键的TextFormField。由于它是一个列表视图,它用相同的表单键多次建立卡片,所以出现了错误。

Duplicate GlobalKey detected in widget tree.

请帮助我解决这个问题。

This is my code.

  final GlobalKey<FormState> _formKey =
  new GlobalKey<FormState>(debugLabel: ' _formKey');
 @override
 Widget build(BuildContext context) {
return  Card(
    margin: EdgeInsets.symmetric(
      horizontal: 15,
      vertical: 4,
    child: Padding(
      padding: EdgeInsets.all(8),
      child: Column(
        children: <Widget>[
          ListTile(
            leading: CircleAvatar(
              backgroundColor: Colors.blue,
              child: Padding(
                padding: EdgeInsets.all(5),
                child: FittedBox(
                  child: Text(
                    widget.price,
                    style: TextStyle(color: Colors.white),
            title: Text('Title'),
            subtitle: Text('Subtitle'),
            trailing: Text(widget.quantity.toString() + ' x'),
          Padding(
              padding: const EdgeInsets.only(
                  left: 32, right: 5, top: 0, bottom: 5),
              child: Container(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    new Form(
                      key: this._formKey,
                      child: new TextFormField(
                        decoration: new InputDecoration(
                            hintText: 'Add your comments',
                            labelText: 'Comments'),
                        // validator: this._validateEmail,
                        onSaved: (String value) {
                          //  _formKey.currentState.save();
    
3 个评论
@NileshRathod 是的,我检查过了,但没有得到确切的解决方案。
你似乎需要为列表中的每一项建立一个不同的GlobalKey。要做到这一点,你必须为每一个listtile建立一个新的GlobalKey。
android
ios
flutter
USER9561
USER9561
发布于 2019-12-23
2 个回答
chunhunghan
chunhunghan
发布于 2022-04-05
已采纳
0 人赞同

你可以复制粘贴运行以下完整的测试代码
您可以使用 List<GlobalKey<FormState>> 并通过数据列表长度生成密钥。
code snippet

  List<GlobalKey<FormState>> _formKey = [];
  @override
  void initState() {
    // TODO: implement initState
    _formKey = new List<GlobalKey<FormState>>.generate(dataList.length,
        (i) => new GlobalKey<FormState>(debugLabel: ' _formKey'));
    super.initState();
  new Form(
            key: this._formKey[index],

working demo

full test code

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        appBarTheme: AppBarTheme(color: Colors.green),
        primarySwatch: Colors.purple,
      home: MyHomePage(),
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
class Data {
  String price;
  int quantity;
  Data({this.price, this.quantity});
class _MyHomePageState extends State<MyHomePage> {
  List<Data> dataList = [
    Data(price: "1", quantity: 2),
    Data(price: "3", quantity: 4)
  List<GlobalKey<FormState>> _formKey = [];
  @override
  void initState() {
    // TODO: implement initState
    _formKey = new List<GlobalKey<FormState>>.generate(dataList.length,
        (i) => new GlobalKey<FormState>(debugLabel: ' _formKey'));
    super.initState();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: AppBar(
          title: Text('Simple Card view'),
        body: new ListView.builder(
            padding: const EdgeInsets.all(16),
            itemCount: dataList.length,
            itemBuilder: (context, index) {
              return Card(
                  margin: EdgeInsets.symmetric(
                    horizontal: 15,
                    vertical: 4,
                  child: Padding(
                      padding: EdgeInsets.all(8),
                      child: Column(children: <Widget>[
                        ListTile(
                          leading: CircleAvatar(
                            backgroundColor: Colors.blue,
                            child: Padding(
                              padding: EdgeInsets.all(5),
                              child: FittedBox(
                                child: Text(
                                  dataList[index].price,
                                  style: TextStyle(color: Colors.white),
                          title: Text('Title'),
                          subtitle: Text('Subtitle'),
                          trailing:
                              Text(dataList[index].quantity.toString() + ' x'),
                        Padding(
                            padding: const EdgeInsets.only(
                                left: 32, right: 5, top: 0, bottom: 5),
                            child: Container(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  new Form(
                                    key: this._formKey[index],
                                    child: new TextFormField(
                                      decoration: new InputDecoration(
                                          hintText: 'Add your comments',
                                          labelText: 'Comments'),
                                      // validator: this._validateEmail,
                                      onSaved: (String value) {
                                        //  _formKey.currentState.save();
                      ])));