본문 바로가기

Flutter/02 Web

[Flutter] Web 기본설정

Firebase 에서 웹 등록 버튼을 클릭 후

 

 

아래의 SDK 부분을 web/index.html 에 추가함

 

 

실제로 개인 설정 파일 내용

- 상세 설정 참고 사이트 ==> https://firebase.flutter.dev/docs/overview

- 이메일 로그인/구글로그인/페이스북 로그인/애플로그인(개발자 계정필요해서 못함)

 

<body>

<!-- The core Firebase JS SDK is always required and must be listed first -->
<!--  <script src="https://www.gstatic.com/firebasejs/8.7.1/firebase-app.js"></script>-->
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
// 버전은 현재 기준 최신버전임, 아래 내용은 개인의 환경에 따라 다름
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-storage.js"></script>

<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/8.7.1/firebase-analytics.js"></script>

<script>
      // Your web app's Firebase configuration
      // For Firebase JS SDK v7.20.0 and later, measurementId is optional
      var firebaseConfig = {
        apiKey: "개인설정에 따라 다름",
        authDomain: "개인설정에 따라 다름",
        projectId: "개인설정에 따라 다름",
        storageBucket: "개인설정에 따라 다름",
        messagingSenderId: "개인설정에 따라 다름",
        appId: "개인설정에 따라 다름",
        measurementId: "개인설정에 따라 다름",
      };
      // Initialize Firebase
      firebase.initializeApp(firebaseConfig);
      firebase.analytics();
  </script>

// 안드로이드 앱에서 보이던 사진들(URL로 로딩하는 사진)이 안보이는 오류 발생시 대처방법
<!-- NetworkImage 함수 오류 해결 -->
<script type="text/javascript">
    let useHtml = true;
    if(useHtml) {
      window.flutterWebRenderer = "html";
    } else {
      window.flutterWebRenderer = "canvaskit";
    }
  </script>

// 아래 부분은 기본 설정부분(flutter 프로젝트 생성시 자동 생성)

  <!-- This script installs service_worker.js to provide PWA functionality to
       application. For more information, see:
       https://developers.google.com/web/fundamentals/primers/service-workers -->
  <script>
    var serviceWorkerVersion = null;
    var scriptLoaded = false;
    function loadMainDartJs() {
      if (scriptLoaded) {
        return;
      }
      scriptLoaded = true;
      var scriptTag = document.createElement('script');
      scriptTag.src = 'main.dart.js';
      scriptTag.type = 'application/javascript';
      document.body.append(scriptTag);
    }

    if ('serviceWorker' in navigator) {
      // Service workers are supported. Use them.
      window.addEventListener('load', function () {
        // Wait for registration to finish before dropping the <script> tag.
        // Otherwise, the browser will load the script multiple times,
        // potentially different versions.
        var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
        navigator.serviceWorker.register(serviceWorkerUrl)
          .then((reg) => {
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener('statechange', () => {
                if (serviceWorker.state == 'activated') {
                  console.log('Installed new service worker.');
                  loadMainDartJs();
                }
              });
            }
            if (!reg.active && (reg.installing || reg.waiting)) {
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing ?? reg.waiting);
            } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log('New service worker available.');
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log('Loading app from service worker.');
              loadMainDartJs();
            }
          });

        // If service worker doesn't succeed in a reasonable amount of time,
        // fallback to plaint <script> tag.
        setTimeout(() => {
          if (!scriptLoaded) {
            console.warn(
              'Failed to load app from service worker. Falling back to plain <script> tag.',
            );
            loadMainDartJs();
          }
        }, 4000);
      });
    } else {
      // Service workers not supported. Just drop the <script> tag.
      loadMainDartJs();
    }
  </script>

</body>

 

'Flutter > 02 Web' 카테고리의 다른 글

[Flutter] Web - Firebase에 hosting 하는 절차  (0) 2021.08.06
[Flutter] Web 설정  (0) 2021.08.06