flutter中使用GetX调用函数时返回的字符串值为null或空

xqk2d5yq  于 2023-01-21  发布在  Flutter
关注(0)|答案(2)|浏览(190)

我想调用字符串函数,在运行应用时使用package_info_plus获取packageName,如下所示:

class SplashScreenController extends GetxController {
  late String one = '';
  late RxString two = ''.obs;

  @override
  void onInit() {
    _initPackageInfo();
    _setImage();
    Timer(Duration(seconds: 5), () => Get.offNamed(Routes.DASHBOARD));
    super.onInit();
  }

  @override
  void onClose() {}

  Future<void> _initPackageInfo() async {
    final info = await PackageInfo.fromPlatform();
    one = info.packageName;
  }

  String _setImage() {
    if (one == 'com.package.one') {
      return two.value = Images.one;
    } else if (one == 'com.package.two) {
      return two.value = Images.two;
    } else {
      return two.value = Images.one;
    }
  }
}

当我尝试在Image.asset()中调用RxString two

@override
  Widget build(BuildContext context) {
    print(controller.two);
    return Obx(() => Scaffold(
            body: Container(
          color: const Color.fromARGB(255, 255, 255, 255),
          alignment: AlignmentDirectional.center,
          child: controller.one.isNotEmpty
              ? Image.asset(controller.two.toString())
              : const SizedBox.shrink(),
        )));
  }

它显示了空值,我是否可以在条件大小写中使用packageName值,然后在Image.asset()小部件中调用它?

fykwrbwg

fykwrbwg1#

我已经重写了你的控制器和UI代码。请检查出来。
控制器(启动画面)

class SplashScreenController extends GetxController {
  late RxString one = ''.obs;
  late RxString two = ''.obs;

  @override
  void onInit() async {
    await _initPackageInfo();
    Timer(Duration(seconds: 5), () => Get.offNamed(Routes.DASHBOARD));
    super.onInit();
  }

  @override
  void onClose() {}

  Future<void> _initPackageSetImage() async {
    final info = await PackageInfo.fromPlatform();
    one.value = info.packageName;
    switch(one.value){
      case 'com.package.one':
        two.value = Images.one;
        break;
      case 'com.package.two':
        two.value = Images.two;
        break;
      default:
        two.value = Images.one;
        break;
    }
  }
}

UI(启动画面)

@override
  Widget build(BuildContext context) {
    
    return Scaffold(
            body: Container(
          color: const Color.fromARGB(255, 255, 255, 255),
          alignment: AlignmentDirectional.center,
          child: Obx((){
             print(controller.two.value);
             if(controller.one.value.isNotEmpty){
               return Image.asset(controller.two.value);
             }
             return const SizedBox.shrink();
           }),
        ));
  }
cngwdvgl

cngwdvgl2#

这实际上与Getx无关,它与Dart语言编程和futures有关,在这段代码中:

@override
      void onInit() {
        _initPackageInfo();
        _setImage();
        Timer(Duration(seconds: 5), () => Get.offNamed(Routes.DASHBOARD));
        super.onInit();
      }

_initPackageInfo()是异步方法,需要时间来解析(因此info变量获得PackageInfo示例),这意味着同步执行它仍将获得示例,但它不会等待它,它将立即运行_setImage(),此时PackageInfo.fromPlatform()尚未得到解析***,因此它是null,您需要做的是让它等待,直到有示例**,然后**继续运行其他代码:

@override
  void onInit() {
    _initPackageInfo().then((val) {
     // this will be executed when _initPackageInfo() finishes.
     _setImage();
    Timer(Duration(seconds: 5), () => Get.offNamed(Routes.DASHBOARD));
   });
  super.onInit();
  }

相关问题