【问题标题】:Passing Constructor value from view to controller in getx在getx中将构造函数值从视图传递到控制器
【发布时间】:2021-08-21 23:16:34
【问题描述】:

所以这是我的视图,其中包含构造函数。我想将此构造函数值产品传递给我的控制器,即 WishListIconController。这个视图实际上是在我当前项目的许多视图中调用的小部件,因此我使用构造函数将值传递给它。

 class WishListIconView extends GetView{
   final products;
   final category;

   WishListIconView({this.products, this.category});
   WishListServiceApi wishListService = locator<WishListServiceApi>();

   WishlistIconController wishListIconController =
   Get.put(WishlistIconController());

  @override
  Widget build(BuildContext context) {
  return Obx(
    () => locator<SharedPreferencesManager>().isKeyExists('isLogin') == true
        ? wishListIconController.isAdded.value == false
            ? IconButton(
                splashRadius: 3,
                constraints: BoxConstraints(),
                padding: EdgeInsets.all(0),
                icon: Icon(
                  Icons.favorite_border,
                  size: 18,
                  color: Colors.blue,
                ),
                onPressed: () async {
                  bool success = await wishListService.addToWishList(
                      products.id, category, products);
                },
              )
            : IconButton(
                splashRadius: 3,
                constraints: BoxConstraints(),
                padding: EdgeInsets.all(0),
                icon: Icon(
                  Icons.favorite,
                  size: 18,
                  color: Colors.blue,
                ),
                onPressed: () async {
                  bool success = await wishListService.deleteWishListItem(
                      products.id, category);
              
                },
              )
        : IconButton(
            splashRadius: 3,
            constraints: BoxConstraints(),
            padding: EdgeInsets.all(0),
            icon: Icon(
              Icons.favorite_border,
              size: 18,
              color: Colors.blue,
            ),
            onPressed: () {
              Fluttertoast.showToast(
                  msg: "Please login to add product to wishlist",
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.TOP,
                  backgroundColor: Colors.blue[300],
                  textColor: Colors.white,
                  fontSize: 16.0);
            },
          ));
   }
 }

这是我的控制器。我想在这里传递产品,以便我可以使用创建函数 产品价值。

 class WishlistIconController extends GetxController {
     WishListServiceApi wishListServiceApi = locator<WishListServiceApi>();
     var isAdded = false.obs;

   @override
  void onInit() {
    super.onInit();
    isInWishList();
  }

 isInWishList() {
   isAdded.value = wishListServiceApi.wishListResponse
    .any((element) => element.id == products.id);

  update();
 }

 @override
  void onReady() {
  super.onReady();
  isInWishList();
   }
 }

【问题讨论】:

    标签: flutter flutter-getx


    【解决方案1】:

    在您的控制器中声明一个products 变量并声明一个init 方法,该方法将设置products 变量的值:

    var products;
    
    init(p){
      products = p;
      update();
    }
    

    然后在您的小部件的构建方法上,在返回您的小部件树之前,将其称为:

    wishListIconController.init(products);
    return....
    

    【讨论】:

    • 感谢您的回答,但我已经使用 getbuilder 来传递该值。
    【解决方案2】:
    *in your controller create constractor like this
    
        class exampleController extends GetxController {
          var user_id;
    
         exampleController (this.user_id);
    ...
    
     void onInit() {
       print(user_id);
      }
    }
    
    and in your widget pass variable like this
    
        class example extends StatelessWidget {
    
         @override
          Widget build(BuildContext context) {
    
           final Controller = Get.put(exampleController("12"));
            return Scaffold(
                 ......
                  )
           }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多