worldint
mathengi
worldint
전체 방문자
오늘
어제
  • 분류 전체보기 (152)
    • infra, cloud (4)
      • aws (4)
    • TIL,WIL(일간,주간 회고) (57)
    • 컴퓨터 공학 (5)
      • 정보통신 (3)
      • 컴퓨터 구조 (2)
    • Math (1)
      • linear algebra (0)
      • 명제와 집합 (1)
    • Operating System (8)
      • Linux Ubuntu (1)
    • programming (66)
      • c , c++ (9)
      • c# (0)
      • java (2)
      • javascript (14)
      • Python (4)
      • github (1)
      • programing terms (12)
      • html, css (2)
      • docker (3)
      • algorithm_datastructure (5)
      • database (11)
      • flutter(dart) (2)
    • 항해99 부트캠프 (7)
      • 사전교육 (7)
    • 보안관련 (1)

블로그 메뉴

    공지사항

    인기 글

    태그

    • Blue/Green
    • MongoDB
    • ec2 #코드디플로이 #리눅스
    • db데드락
    • flutter #provider #error
    • Javascript
    • node
    • NVM
    • AWS
    • ci/cd
    • MONGOOSE
    • docker
    • 디비데드락
    • NoSQL
    • nodejs
    • EC2
    • CloudFront

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    worldint

    mathengi

    [flutter] Could not find the correct Provider<ProviderClass> above this <WidgetName> Widget 에러 해결
    programming/flutter(dart)

    [flutter] Could not find the correct Provider<ProviderClass> above this <WidgetName> Widget 에러 해결

    2024. 5. 8. 11:21

    아직 flutter 학습이 부족하여 내용이 잘못 되었을 수도 있습니다 그냥 해결 방법 정도로 참고만 해주세요

     

     

    1. 상황 : 

    외주 받아서 만들고 있는 앱에서
    flutter_blue 패키지를 사용하여 blue tooth를 스캔하고 연결한 뒤에
    blueTooth 서비스 정보를 Provider에 담아서 다른 위젯에서 사용하는 코드를 작성중이다

    아래 코드는 home_body.dart 위젯에서 버튼을 클릭하면
    bleScan.dart 위젯으로 Navigator.push()를 사용해서 이동하는 코드인데

    onPressed: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => Provider.value(
                                value: Provider.of<BlueToothProvider>(context
                                    ),
                                child: BleScan())));
                  }

     

    Navigator.push를 사용하면 하위 위젯이 아니고 새롭게 생성된 위젯이어서 

    이렇게 HomeScreen 외부에 BleScan이 생겨난다

    그래서 BleScan 위젯에서 바로 Provider.of 로 접근 할 수 없기 때문에
    위 코드처럼 Provider.value를 사용해서

    이미 생성된 provider객체를 라우팅되는 위젯쪽으로 넘겨주면 될 것으로 생각했다

    Could not find the correct Provider<ProviderClass> above this Builder Widget 발생

    그런데 이런 에러가 또 발생함

    2. 원인 : 

    1. 저 MaterialPateRoute 안쪽의 builder매개변수로 전달된 callback함수에서는
      HomeScreen(상위위젯)에서 create한 Provider에 접근이 안되는것으로 보인다.
      ( 새로운 context 저 지점에서 생성되어서 HomeScreen의 context와 다른 context로 판단됨 ) 
    2. 그래서 저렇게 value를 사용해서 기존에 있는 객체를 넘겨주려 하면 못 찾겠다는 에러가 발생하는 것으로 보인다

     

    3. 결론 : 

    두가지 방법이 있는데 

    1. HomeScreen이 아닌 더 상위에 있는 MaterialApp을 ChangeNotifierProvier로 감싸줘서
    전역 provider로 어느 곳 에서 든지 다 사용 할 수 있게 하거나



    2. 아래 코드처럼 create을 해서 다시 넘겨주는것

    MaterialPageRoute(
    	builder: (context) => ChangeNotifierProvider(
    		create: (BuildContext context) => BlueToothProvider(),
    		child: BleScan()
            )
    	)
    );

     

     

     

     

    'programming > flutter(dart)' 카테고리의 다른 글

    [flutter] android apk 파일 만드는법  (0) 2024.05.07
      'programming/flutter(dart)' 카테고리의 다른 글
      • [flutter] android apk 파일 만드는법
      worldint
      worldint
      공부한 내용들, 트러블 슈팅 용 블로그

      티스토리툴바