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<Type>),Type可以是自定义对象类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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>(User(name: "张三", age: 18)); class User { User({required String name, required int age}); var name; var age; }
方式三( 变量末尾添加.obs)
1 2 3 4 5 6 7 8 9 10
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(name: '张三',age: 18).obs;
对象实现obs的方式(User)
1-让对象中的每个变量都实现obs
1 2 3 4
class RxUser { final name = "Camila".obs; final age = 18.obs; }