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

Duplicate GlobalKey detected in widget tree in foreach

我有一张带有标记循环的地图,对于每个标记,我都尝试显示带有位置名称的小吃店。我有一个类似的代码。

     static final GlobalKey<ScaffoldState> scaffoldKey =
              GlobalKey<ScaffoldState>();
     for (int i = 0; i < list.length; i++) {
            markers.add(new Marker(
                width: 80.0,
                height: 80.0,
                point: list[1].values.elementAt(i),
                builder: (ctx) => new Container(
                    child: new GestureDetector(
                        onTap: () {
                          scaffoldKey.currentState.showSnackBar(new SnackBar(
                            duration: const Duration(seconds: 5),
                            content: new Text(coords.keys.elementAt(i)),
                            action: SnackBarAction(
                              label: 'Ver',
                              onPressed: () {
                                // Some code to undo the change!
                              },
                            ),
                          ));
                        },
                        child: new Icon(Icons.home, color: Colors.red[300])))));
          }
        return new Scaffold(
           key: scaffoldKey,

当我加载此页面时没有加载任何内容和控制台 return

Duplicate GlobalKey detected in widget tree.

我认为错误出在脚手架上,对于每次迭代,它都试图设置相同名称的 globalkey。所以当它执行 foreach 时,我需要用不同的名称来识别这个键。知道我该怎么做吗?

只需从您的 final GlobalKey<ScaffoldState> 声明中删除 static 修饰符。

 final GlobalKey<ScaffoldState> scaffoldKey =
          GlobalKey<ScaffoldState>();