当我进行热重启时,productsController 没有启动,但是当我进行热重载时,它会工作,颤振 Getx

productsController is not initiated when i do hot restart , but when i do hot reload it works , flutter Getx

我正在开发电子商务应用程序,我正在使用 Getx 进行状态管理,我是一个新手 Getx 如果能提供一点帮助,我们将不胜感激。

我正在使用 firebase auth,因此当从 login screen 导航应用程序时一切正常,即 products are shown, 但是当用户 kept logged inproducts are not shown 直到 hard reloaded

main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp().then((value) {
    Get.put(ProductsController());
  });

  runApp(MyApp());
}


@override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: routes,
      home: StreamBuilder(
          stream: FirebaseAuth.instance.authStateChanges(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              if (snapshot.hasData) {
                return HomeScreen();
              }
            }
            return const LoginScreen();
          }),
    );

controllers.dart

ProductsController productsController = ProductsController.instance;

home.dart

    class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
@override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(),
        body: SingleChildScrollView(
          child: Padding(
            padding: EdgeInsets.only(bottom: 20),
            child: Column(
              children: [
                Text("Popular Products"),
                PopularProducts(),
              ],
            ),
          ),
        )
        );
  }

热门产品

这些产品在hot reload后才显示,一开始是不显示的,这个跟productController

有关
class PopularProducts extends StatelessWidget {
  const PopularProducts({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
          child: SizedBox(
            height: 150,
            child: ListView(
                scrollDirection: Axis.horizontal,
                children:
                    productsController.products.map((ProductModel product) {
                  print("These are the products" + product.toString());
                  return ProductCard(product: product);
                }).toList()),
          ),
        )
      ],
    );
  }

图像是: 热重启时:

热重载时:

我遇到了类似的问题,我的应用加载数据的速度不够快,而且小部件已经呈现,所以我创建了一个函数来加载数据,在构建中我使用了 Obx() 小部件并调用了函数,但要记住,但是用变量条件调用的函数是空的

@LMech 回复 Obx() 丢失了!!

现在代码是

@override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Obx(                         --> Add this line
          () => Expanded(
            child: SizedBox(
              height: 150,
              child: ListView(
                  scrollDirection: Axis.horizontal,
                  children:
                      productsController.products.map((ProductModel product) {
                    print("These are the products" + product.toString());
                    return ProductCard(product: product);
                  }).toList()),
            ),
          ),
        )
      ],
    );
  }