Closed boostmerlin closed 8 months ago
Hi @boostmerlin!
You can't reuse GlobalKey
in Flutter. It must be unique.
I believe you are doing something like this:
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyWidget(),
);
}
}
class MyWidget extends StatelessWidget {
const MyWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// don't do that
GlobalKey sameGlobalKey = GlobalKey();
return Scaffold(
body: Center(
key: sameGlobalKey,
child: Container(
key: sameGlobalKey,
color: Colors.blue,
width: 100,
height: 100)));
}
}
FlutterError (Multiple widgets used the same GlobalKey. The key [GlobalObjectKey String#a3a86] was used by 2 widgets:
Positioned-[GlobalObjectKey String#a3a86]
SceneView-[GlobalObjectKey String#a3a86]
A GlobalKey can only be specified on one widget at a time in the widget tree.)