반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

welcome to my blog

[Flutter] GetX Error - [Get] the improper use of a GetX has been detected. 본문

Flutter

[Flutter] GetX Error - [Get] the improper use of a GetX has been detected.

_annie_ 2021. 11. 18. 20:00
728x90

에러 : 

  [Get] the improper use of a GetX has been detected. 
      You should only use GetX or Obx for the specific widget that will be updated.
      If you are seeing this error, you probably did not insert any observable variables into GetX/Obx 
      or insert them outside the scope that GetX considers suitable for an update 
      (example: GetX => HeavyWidget => variableObservable).
      If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.

 

내 코드 : 

class _HomeViewState extends State<HomeView> {

  @override
  Widget build(BuildContext context) {    
      return GetX<HomeController>(
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(
                title: Text('ABCD'),
              ),
              body: Column(
                children: [
                  Text('123123'),
                  TextButton(onPressed: (){
                  }, child: Text('EFG'))
                ],
              )
          );
        },);
  }
}

그냥 Scaffold만 있었을때에는 에러가 나지 않다가 

상위에 GetX<controller>를 추가하기만 하면 위와 같은 에러가 발생.

<구글번역>

[Get] GetX의 부적절한 사용이 감지되었습니다.
업데이트할 특정 위젯에 대해서만 GetX 또는 Obx를 사용해야 합니다.
이 오류가 표시되면 GetX/Obx에 관찰 가능한 변수를 삽입하지 않았을 수 있습니다.
또는 GetX가 업데이트에 적합하다고 생각하는 범위 밖에 삽입하십시오.
(예: GetX => HeavyWidget => variableObservable).
상위 위젯과 하위 위젯을 업데이트해야 하는 경우 각각을 Obx/GetX로 래핑합니다.

 

항상 느끼는거지만 한글인데도 한번에 이해가 안감

쨋든 업데이트 할 특정 위젯에 대해서만 사용해야 된다 함

 

추가로 구글검색하다가 아래와 같은 답변을 찾음 (비슷한 뜻인듯)

 

문제 : 해당 위젯에서 관찰가능한 변수를 사용하지 않기 때문에 발생

해결 : GetX로 연결된 controller에 있는 변수를 해당 위젯에서 사용하면 됨

 

그니까 관찰가능한변수(obs로 초기화된)가 사용되지않으니 GetX<controller> 쓸필요없다 에러! 이런것인듯

 

HomeController에 다음과 같은 변수를 선언하고 (obs - 값 관찰가능한 값)

RxInt count = 0.obs;

 

@override
  Widget build(BuildContext context) {
    // SettingController settingController = Get.find();
      return GetX<HomeController>(
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(
                title: Text('${controller.count}'),
              ),
              body: Column(
                children: [
                  Text('123123'),
                  TextButton(onPressed: (){
                  }, child: Text('EFG'))
                ],
              )
          );
        },);
  }

아무 Text위젯에다가 해당 변수를 연결해주었더니 정상적으로 동작됨

 

728x90
반응형