LateError was thrown building Obx(has builder, dirty, state: _ObxState#17527): LateInitializationError: Field 'products' has not been initialized

LateError was thrown building Obx(has builder, dirty, state: _ObxState#17527): LateInitializationError: Field 'products' has not been initialized

我正在使用 GetX。我需要显示应用程序打开的数据库中的数据。但我得到了例外。

The following LateError was thrown building Obx(has builder, dirty, state: _ObxState#17527): LateInitializationError: Field 'products' has not been initialized.

尽管如此,我已经在 onInit 中初始化了 late 变量。

控制器代码为:

class HomeController extends GetxController {
  HomeController({required this.homeRepository});
  final IHomeRepository homeRepository;
  late Rx<Either<FireStoreServerFailures, List<Product>>> products; //=
  @override
  void onInit() async {
  products.value=  await fetchProductsFromDB();
    super.onInit();
  }
 Future<Either<FireStoreServerFailures, List<Product>>> fetchProductsFromDB() async {
    var _completer=Completer<Either<FireStoreServerFailures, List<Product>>>();
    homeRepository.fetchProducts().listen((event) {
      _completer.complete(event);
    });
    return await _completer.future;
  }
}

我使用的主页 后期变量的代码是:

 Obx(
      () => GridView.builder(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            childAspectRatio: 2 / 2,
          ),
          // TODO Deal with errors
          itemCount: controller.products.value.getOrElse(() => []).length,
          itemBuilder: (context, index) {
            return controller.products.value.fold(
                    (l) => Container(
                          height: double.infinity,
                          width: double.infinity,
                          color: Colors.red,
                          child: Text("Error ${l.msg}"),
                        ),
                    (r) => Card(
                          elevation: 2.0,
                          color: Colors.amber,
                          child: InkWell(
                            onTap: () async =>
                                await controller.goToMoreDetails(),
                            child: Stack(
                              fit: StackFit.expand,
                              clipBehavior: Clip.hardEdge,
                              children: [
                                Image.network(
                                  controller.products.value.fold(
                                      (l) => "",
                                      (r) => r[index]
                                          .pickedImages
                                          .getOrCrash()[0]),
                                  fit: BoxFit.fill,
                                  height: 200,
                                  width: 200,
                                ),
                                OverflowBox(
                                  minHeight: 30,
                                  alignment: Alignment.bottomCenter,
                                  child: Container(
                                    color: Colors.black.withOpacity(0.7),
                                    height: 30,
                                    width: double.infinity,
                                    child: Center(
                                      child: Text(
                                        "${controller.products.value.fold((l) => "", (r) => r[index].price.getOrCrash())}",
                                        style: const TextStyle(
                                          color: Colors.white,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )) ;
          }),
    );

您声明了 late Rx<Either<FireStoreServerFailures, List<Product>>> products; 但没有在任何地方初始化 products。但是在您的 onInit 方法中,您尝试访问尚未初始化的 products 上的 .value。设置 products.value 不是初始化 products。此外,您的 Obx 小部件尝试访问 products 字段,该字段在它尝试访问时尚未初始化。

你应该做的是:

  final products = Rxn<Either<FireStoreServerFailures, List<Product>>>();

在你的UI中:

 Obx(
  () => controller.products.value == null? CircularProgressIndicator() : GridView.builder(......