flutter

Flutter Getx 声明一个响应式变量三种方式

2024-05-09  本文已影响0人  __Mr_Xie__

第一种 使用Rx{Type}

final name = RxString('');
final isLogged = RxBool(false);
final count = RxInt(0);
final balance = RxDouble(0.0);
final items = RxList<String>([]);
final myMap = RxMap<String, int>({});

第二种是使用Rx,规定泛型Rx

final name = Rx<String>('');
final isLogged = Rx<Bool>(false);
final count = Rx<Int>(0);
final balance = Rx<Double>(0.0);
final number = Rx<Num>(0)
final items = Rx<List<String>>([]); 
final myMap = Rx<Map<String, int>>({}); 

//自定义类 - 可以是任何类
final user = Rx<User>();

第三种 更实用、更简单、更可取的方法,只需添加.obs作为value的属性。(推荐)

final name = ''.obs;
final isLogged = false.obs;
final count = 0.obs;
final balance = 0.0.obs;
final number = 0.obs;
final items = <String>[].obs; 
final myMap = <String, int>{}.obs; 

//自定义类 - 可以是任何类
final user = User().obs;
上一篇下一篇

猜你喜欢

热点阅读