键[GlobalObjectKey int#a1467]被多个小部件使用



我有一个项目,每个页面上都有很长的表格代码,没有Bloc或其他代码分离。我有这个代码,必须打开对话框与位置偷看在一个页面:

showDialog(
context: context,
builder: (BuildContext context) {
return LocationPickerDialog(_address);
}).then....

点击后,出现bug。在项目no GlobalKeys中。在其他页面中,此代码不会出错。

The key [GlobalObjectKey int#a1467] was used by multiple widgets. The parents of those widgets were different widgets that both had the following description:
Builder(dependencies: [MediaQuery])
A GlobalKey can only be specified on one widget at a time in the widget tree.

如何理解为什么会发生这种情况?

是什么部件造成的?

更新:

class LocationPickerDialog extends StatefulWidget {
var retrieveAddress;
LocationPickerDialog(this.retrieveAddress);
@override
_LocationPickerDialogState createState() =>
new _LocationPickerDialogState(retrieveAddress);
}
class _LocationPickerDialogState extends State<LocationPickerDialog> {
var retrieveAddress;
_LocationPickerDialogState(this.retrieveAddress);
final searchLinkHolder = TextEditingController();
final addressHolder = TextEditingController();
var searchLink;
var _address;
var Address = "";
List<String> placeList = [];
var Locationlist = <beanLocation>[];
@override
void initState() {
super.initState();
}
getSuggestions(String query) {
if (query.isNotEmpty) {
API.getPlaceSearchAPI(query).then((response) {
setState(() {
Iterable list = json.decode(response.body)['features'];
Locationlist =
list.map((model) => beanLocation.fromJson(model)).toList();
});
});
setState(() {
placeList.clear();
for (int i = 0; i < Locationlist.length; i++) {
Constant_Class.printMessage(
"Locationlist => $i" + Locationlist[i].name.toString());
placeList.add(Locationlist[i].name);
}
});
} else {
Constant_Class.printMessage("Locationlist => 123");
}
}
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
// designSize: Constant_Class.screenSize(context),
builder: (_) => Scaffold(
backgroundColor: Colors.transparent,
resizeToAvoidBottomInset: false,
body: Stack(
children: [
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Container(
color: Colors.black.withOpacity(0.5),
),
),
Container(
margin: EdgeInsets.only(right: 20, left: 20, bottom: 20, top: 20),
child: Container(
decoration: BoxDecoration(
borderRadius: new BorderRadius.all(const Radius.circular(30)),
color: Colors.white,
),
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Align(
alignment: Alignment.topRight,
child: InkWell(
onTap: () {
Navigator.pop(context);
},
child: Container(
margin:
EdgeInsets.only(right: 10, top: 5, bottom: 5),
height: 35,
width: 35,
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: ColorsApp.colorSelectedTab),
child: Center(
child: Icon(
Icons.close_rounded,
color: Colors.white,
),
)),
),
),
Container(
margin: EdgeInsets.all(5),
padding: EdgeInsets.all(12),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius:
new BorderRadius.all(const Radius.circular(30)),
border: Border.all(color: Colors.black, width: 1.0)),
child: TextFormField(
controller: searchLinkHolder,
textAlignVertical: TextAlignVertical.center,
textInputAction: TextInputAction.done,
cursorColor: Colors.black,
style: TextStyle(
fontFamily: Constant_Class.appFontFamily,
color: Colors.black),
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(top: 3),
filled: false,
//fillColor: Theme.of(context).cardColor,
labelStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.black),
hintStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.grey),
hintText: 'google_meet'.tr()),
keyboardType: TextInputType.text,
onSaved: (val) {
searchLink = val;
},
onChanged: (val) {
searchLink = val;
},
onFieldSubmitted: (value) {
// if(value != null && value.trim() != ""){
//   Navigator.of(context).pop(value);
// }
setState(() {
searchLink = value;
});
if (value != null && Uri.parse(value).host != "") {
Constant_Class.locationLatitude = "";
Constant_Class.locationLongitude = "";
Constant_Class.printMessage(value);
Navigator.of(context).pop(value);
} else {
Constant_Class.ToastMessage(
'Please enter valid url');
}
},
),
),
SizedBox(
height: 10,
),
_textWithRightLeftLine('or'.tr()),
SizedBox(
height: 10,
),
Container(
margin: EdgeInsets.all(5),
padding: EdgeInsets.all(12),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius:
new BorderRadius.all(const Radius.circular(30)),
border: Border.all(color: Colors.black, width: 1.0)),
child: TextFormField(
controller: addressHolder,
textAlignVertical: TextAlignVertical.center,
textInputAction: TextInputAction.done,
cursorColor: Colors.black,
style: TextStyle(
fontFamily: Constant_Class.appFontFamily,
color: Colors.black),
decoration: new InputDecoration(
border: InputBorder.none,
focusedBorder: InputBorder.none,
enabledBorder: InputBorder.none,
errorBorder: InputBorder.none,
disabledBorder: InputBorder.none,
isDense: true,
contentPadding: EdgeInsets.only(top: 3),
filled: false,
//fillColor: Theme.of(context).cardColor,
labelStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.black),
hintStyle: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontSize: 16,
fontWeight: FontWeight.normal,
color: Colors.grey),
hintText: 'search_location'.tr()),
keyboardType: TextInputType.text,
onSaved: (val) {
_address = val;
getSuggestions(val.toString());
},
onChanged: (val) {
_address = val;
getSuggestions(val.toString());
},
onFieldSubmitted: (value) {
Constant_Class.locationLatitude = "";
Constant_Class.locationLongitude = "";
setState(() {
_address = value;
});
if (searchLink != null &&
Uri.parse(searchLink).host != "") {
Constant_Class.printMessage(searchLink);
Navigator.of(context).pop(searchLink);
} else {
Navigator.of(context).pop(_address);
}
},
),
),
Expanded(
child: ListView.separated(
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
shrinkWrap: true,
itemCount: placeList.length,
separatorBuilder: (BuildContext context, int index) =>
Divider(),
itemBuilder: (BuildContext context, int index) {
return ListTile(
dense: true,
title: InkWell(
onTap: () {
Constant_Class.locationLatitude =
Locationlist[index].latitude;
Constant_Class.locationLongitude =
Locationlist[index].longitude;
Navigator.of(context)
.pop(placeList[index].toString());
},
// child: Text(placeList[index].toString()),
child: Text(
placeList[index].toString(),
textAlign: TextAlign.left,
style: TextStyle(
fontFamily: Constant_Class.appFontFamily,
fontWeight: FontWeight.w500,
color: Colors.black,
fontSize: 16),
),
),
);
},
),
),
],
),
),
),
],
),
),
);
}
Widget _textWithRightLeftLine(var title) {
return Container(
child: Column(
children: [
Row(
children: [
Expanded(
child: new Container(
margin: const EdgeInsets.only(left: 0.0, right: 10.0),
child: Container(
color: ColorsApp.colorTextLight,
height: 1,
)),
),
Text(
title,
style: TextStyle(
color: ColorsApp.colorTextDark,
fontFamily: Constant_Class.appFontFamily),
),
Expanded(
child: new Container(
margin: const EdgeInsets.only(left: 10.0, right: 0.0),
child: Container(
color: ColorsApp.colorTextLight,
height: 1,
)),
),
],
),
],
),
);
}
}

https://drive.google.com/file/d/1KRidmPbZJeobB6FCG2TZmn1ENFvkAojj/view?usp=sharing

我知道是什么了。在这个屏幕和对话框中的根小部件是来自flutter_screenutil包的ScreenUtilInit。我把它从一页上删除了,问题就解决了。

最新更新