Bog*_*byk 1 dart flutter flutter-getx
假设我有一个这样的控制器:
class ProfileController extends GetxController {
Rx<UserFacebookInfo?> facebookInfo = null.obs;
void facebookSync() async {
//
// logic to get user info from facebook
//
facebookInfo.value = UserFacebookInfo.fromFacebookApi(userData);
// facebookInfo = UserFacebookInfo.fromFacebookApi(userData).obs; <=== also tried this
update();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在小部件中我有这样的东西:
Widget buildFacebook() => Padding(
padding: const EdgeInsets.only(top: 30.0, right: 20.0, left: 20.0, bottom: 10.0),
child: Obx(() => (_profileController.facebookInfo.value == null) ? Column(
children: [
IconButton(
icon : const Icon(Icons.facebook_outlined, size: 40.0),
onPressed: () => _profileController.facebookSync()
),
const Text(
'Facebook',
style: TextStyle(color: Colors.white),
)
],
) :
Column(
children: [
UserProfileAvatar(
avatarUrl: _profileController.facebookInfo.value!.facebookAvatar,
radius: 40,
),
Text(
_profileController.facebookInfo.value!.name,
style: const TextStyle(color: Colors.white),
)
],
)
));
Run Code Online (Sandbox Code Playgroud)
并且因为初始值可为空,所以它不起作用并且不会动态更改小部件,但前提是我从 android studio 更新它。正确的初始化方法是什么???我有类似的情况与可空字符串可观察,所以我能够初始化它谎言String string (空作为字符串?).obs`感谢您的任何建议
您可以使用 来初始化可为空的可观察量Rxn<Type>()。
因此使用这个:
final facebookInfo= Rxn<UserFacebookInfo>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2964 次 |
| 最近记录: |