본문 바로가기

Mobile/Flutter

Firebase Realtime Database with Flutter

 

Image Credit: Google

 

Flutter에서 Firebase Realtime Database를 사용하는 방법에 대해서는 아래의 링크(이하 링크)를 참조하면 된다.

https://medium.com/47billion/how-to-use-firebase-with-flutter-e4a47a7470ce

그런데, 위의 방법대로 해보시면 알겠지만, 여러가지 오류발생하는데, 이 포스트에서는 링크에서 빠트린 설정에 대해서만 언급한다.

 

1. Firebase Console에서 Realtime Database Rule설정

링크에서는 Firebase Console에 들어가서 앱에 대한 설정만 하고 마는데, Realtime Database에 대한 권한 설정이 추가로 필요하다.

Firebase Console > Database > Realtime Database > Rules 탭에 가서 아래와 같이 변경해준다.

{
  /* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
  "rules": {
    ".read": true,
    ".write": true
  }
}

2. config file 복사하기

google-services.json 은 android/app 밑에 복사하고, Google-Service.plist 파일은 ios/Runner 에 복사한다. 그리고, 여기서 중요한 건 iOS의 경우, ios/Runner/Runner.xcodeproj 을 Xcode에서 열어서 Goolge-Service.plist를 프로젝트에 반드시 추가해주어야 한다.

3. android project 설정

마지막으로 Android 프로젝트에서 다음과 같은 설정이 추가로 필요하다.

android/app/build.gradle 파일 맨 아래에 다음의 내용을 추가한다.

// Add the following line to the bottom of the file:
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

android/build.gradle 파일의 내용에 다음과 같은 내용을 삽입한다.

buildscript {
    // 1.2.x로 되어 있다면 아래와 같이 올리세요.
    ext.kotlin_version = '1.3.41'

    ...

    dependencies {
        ...
        classpath 'com.google.gms:google-services:3.2.1'  // Google Services plugin
    }
}

자, 여기까지 해 놓으면 이제서야 예제가 제대로 동작합니다. 

Enjoy Firebase and Flutter!