Aka*_*han 5 flutter flutter-getx
我是 Flutter GetX 包的新手,在使用 flutter GetX 包时遇到问题。我有几个应用程序屏幕,其中一个屏幕用于列出数据库中的所有产品。
在我的产品控制器中,我从数据库中获取所有数据并使用列表视图显示,如下面给出的代码所示,并且工作正常。
问题:当我从另一个控制器插入新记录并返回产品列表控制器时,它不会显示新添加的数据。实际上这次onInit方法不会触发。
控制器代码
class ProductIndexCtrl extends GetxController {
var products = [].obs;
@override
void onInit() {
super.onInit();
getAll();
}
void getAll() {
Product.getAll().then((jsonList) {
products.value = Product.fromJsonList(jsonList);
});
}
}
class ProductCreateCtrl extends GetxController {
void saveData(Product product) {
...
...
//after successful insert
Get.toNamed('productIndex');
}
}
Run Code Online (Sandbox Code Playgroud)
产品索引屏幕
final ctrl = Get.put(ProductIndexCtrl());
GetX<ProductIndexCtrl>(builder: (controller) {
return _listview(controller.products);
}
Widget _listview(data) {
...
...
}
Run Code Online (Sandbox Code Playgroud)
由于 GetX 依赖项管理器控制依赖项的生命周期,因此您不应手动调用它们。GetX 负责何时致电他们。
因此,您需要在您喜欢的方法中手动调用控制器getAll()的方法:ProductIndexCtrlsaveData()ProductCreateCtrl
saveData(Product product){
....
....
final indexCtrl= Get.find<ProductIndexCtrl>();
indexCtrl.getAll();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7404 次 |
| 最近记录: |