如何解决使用 Obx,得到这个错误:[Get] 检测到 GetX 的不当使用
请帮帮我。我做错了什么?我有错误:
[Get] the improper use of a GetX has been detected.
这是代码:
class MealRecipesItem extends StatefulWidget {
const MealRecipesItem({
Key? key,@required this.gender,this.item,}) : super(key: key);
final int? gender;
final Data? item;
@override
_MealRecipesItemState createState() => _MealRecipesItemState(item?.id);
}
class _MealRecipesItemState extends State<MealRecipesItem> {
final itemId;
_MealRecipesItemState(this.itemId) {
Get.put(RecipeDetailController(),tag: itemId);
}
@override
Widget build(BuildContext context) {
var controller = Get.find<RecipeDetailController>(tag: itemId);
_toggleFavorite() {
controller.toggleFavorite(widget.item?.databaseId);
}
return Material(
child: InkWell(
onTap: _toggleFavorite,child: Container(
child: Obx(() { // ====**** ERROR IN THIS LINE ****====
if (controller.isLoading) {
return CircularProgressIndicator();
}
if (controller.resultToggleFavorite?.isUserFavorite ?? false) {
return Icon(
Icons.favorite,);
}
return Icon(
Icons.favorite_border,);
}),),);
}
}
在我的情况下使用 Obx 的正确方法是什么?正确的代码是什么?
感谢您的帮助。
解决方法
您的控制器变量似乎都不是可观察的(例如,isLoading
)。它们不是 Rx 类型,而是普通的 dart 类型。 Obx 或 GetX 仅用于 observables(.obs) 或 Rx 变量。它们就像 StreamBuilder。
因此,如果您不使用 observable,则无需使用 Obx 或 GetX。或者,您可以将变量设为可观察的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。