Flutter:多个小部件使用相同的GlobalKey

问题描述 投票:0回答:1

[运行此代码时出现错误:Multiple widgets used the same GlobalKey.因此,我可以解决此问题。我如何将键动态传递给listview.Builder。有可能通过吗?这是我的代码的简化版本:


  GlobalKey<AutoCompleteTextFieldState<String>> key0 = new GlobalKey();

  @override
  Widget build(BuildContext context) {
    return M Scaffold(
        appBar: appBar,
        body: SingleChildScrollView(
          child: Container(
            child: ListView.builder(
               itemCount: 3,
               itemBuilder: (context, index) {
                return  SimpleAutoCompleteTextField(
                  key: key0,
                  suggestions: suggestions,
                  textChanged: (text) => print(text),
                  clearOnSubmit: false,
                  textSubmitted: (text) => print(text)
                ),
              }
            ),
          ),
        ),

    );
  }
flutter dart flutter-layout
1个回答
0
投票

将索引作为您的值

  key: ValueKey(index),
© www.soinside.com 2019 - 2024. All rights reserved.