【问题标题】:I can't usa Obx to update my state page. How can i?我不能使用 Obx 来更新我的状态页面。我怎样才能?
【发布时间】:2022-01-10 16:28:43
【问题描述】:

我有这个错误“ [Get] 检测到对 GetX 的不当使用。 您应该只对将要更新的特定小部件使用 GetX 或 Obx。 如果您看到此错误,您可能没有在 GetX/Obx 中插入任何可观察变量 或将它们插入 GetX 认为适合更新的范围之外 (例如:GetX => HeavyWidget => variableObservable)。 如果您需要更新父小部件和子小部件,请将每个小部件包装在 Obx/GetX 中。 "

我有这个页面

import 'package:get/get.dart';
import 'package:spotify_clone/application/themes/theme_config.dart';
import 'package:spotify_clone/modules/home/home_controller.dart';
import 'package:spotify_clone/modules/home/widget/components/page_two_components.dart/search_bar.dart';
import 'package:spotify_clone/modules/home/widget/components/page_two_components.dart/search_custom_delegate.dart';
import 'package:spotify_clone/modules/home/widget/components/page_two_components.dart/section.dart';

class PageTwo extends GetView<HomeController> {
  const PageTwo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                title: Text(
                  "Buscar",
                  style: ThemeConfig().getTextStyle(fontSize: 24),
                ),
              )
            ];
          },
          body: ListView(
            shrinkWrap: true,
            children: [
              GestureDetector(
                onTap: () => showSearch(
                  context: context,
                  delegate: SearchCustomDelegate(),
                ),
                child: const SearchBar(),
              ),
              Obx(() => Section(
                    title: "Navegar por todas seções",
                    items: controller.allSections,
                  )),
            ],
          ),
        ),
      ),
    );
  }
}

在控制器内部,我声明了“allSections”,例如: RxList&lt;SectionItemModel&gt; allSections = &lt;SectionItemModel&gt;[].obs;

【问题讨论】:

    标签: flutter dart flutter-layout flutter-dependencies flutter-getx


    【解决方案1】:

    Obx() 仅在内部有 GetxController 的扩展类时使用。 我只能看到 HomeController。但我看不到您在 PageTwo 中初始化 controller 的位置。

    【讨论】:

      【解决方案2】:
      import 'package:flutter/material.dart';
      import 'package:get/get.dart';
      import 'package:spotify_clone/application/themes/theme_config.dart';
      import 'package:spotify_clone/modules/home/home_controller.dart';
      import 'package:spotify_clone/modules/home/widget/components/page_two_components.dart/search_bar.dart';
      import 'package:spotify_clone/modules/home/widget/components/page_two_components.dart/search_custom_delegate.dart';
      import 'package:spotify_clone/modules/home/widget/components/page_two_components.dart/section.dart';
      
      class PageTwo extends GetView<HomeController> {
        const PageTwo({Key? key}) : super(key: key);
      
        @override
        Widget build(BuildContext context) {
          return GetMaterialApp(
            home: Scaffold(
              body: SafeArea(
                child: NestedScrollView(
                  headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
                    return <Widget>[
                      SliverAppBar(
                        title: Text(
                          "Buscar",
                          style: ThemeConfig().getTextStyle(fontSize: 24),
                        ),
                      )
                    ];
                  },
                  body: ListView(
                    shrinkWrap: true,
                    children: [
                      GestureDetector(
                        onTap: () => showSearch(
                          context: context,
                          delegate: SearchCustomDelegate(),
                        ),
                        child: const SearchBar(),
                      ),
                      Obx(() => Section(
                        title: "Navegar por todas seções",
                        items: controller.allSections,
                      )),
                    ],
                  ),
                ),
              ),
            ),
          );
        }
      }
      Inside controller i declared "allSectio
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        相关资源
        最近更新 更多