일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 바인딩
- MVVM
- React JS
- 애니메이션
- db
- HTML
- JavaScript
- page
- listview
- Maui
- 오류
- 깃허브
- Flutter
- 자바스크립트
- Animation
- 플러터
- Binding
- 함수
- MSSQL
- 닷넷
- MS-SQL
- AnimationController
- spring boot
- 파이어베이스
- 리엑트
- Firebase
- .NET
- GitHub
- typescript
- 마우이
- Today
- Total
개발노트
53. [Flutter] Firebase Function 기능 활용하기 본문
Flutter에 Firebase Functions 설치하기
1. Firebase 프로젝트에 function 기능을 활성화시켜줍니다.
- Firebase에 Fuction 기능 추가하기: https://mroh1226.tistory.com/163
7.[Firebase] Function 기능 사용하기
Firebase Functions Firebase Functions은 Google의 Firebase 플랫폼에서 제공하는 서버리스 백엔드 서비스입니다. Firebase Functions은 클라우드 기반의 함수를 만들고 실행할 수 있는 도구로, 백엔드 코드를 작성
mroh1226.tistory.com
2. Flutter project에 cloud_functions 패키지 추가하기(터미널에 아래 명령어 입력하여 추가해줍니다.)
flutter pub add cloud_functions
3. flutterfire configure로 설정 적용하기(아래 명령어로 설정을 적용합니다.)
flutterfire configure
4. Firebase CLI 실행하고, 아래 절차대로 Flutter 프로젝트에 functions를 적용합니다.
4-1) firebase init functions 명령어 실행
firebase init functions
> firebase init functions 명령어 입력
> Are tou ready to proceed? (진행하시겠습니까?) > 'y'입력
> Use an existing project 선택 (존재하는 프로젝트에 사용)
> 설치할 Project 선택
> What language would you like to use to write Cloud Functions? (사용할 언어 선택 / 예시로 TypeScript 선택)
> Do you want to use ESLint to catch probable bugs and enforce style?
(ESLint 로 버그 가능성이 높은 부분을 찾겠습니까?) > 'n' 입력
> Do you want to install dependencies with npm now?
(의존성 설치를 위해 npm을 설치하시겠습니까?) > 'y' 입력 > 설치완료!
※프로젝트 폴더에 functions 라는 폴더가 생기면 정상 설치된 것입니다.
예시 소스.
index.ts
//firebase-functions: Firebase Cloud Functions를 사용하기 위한 모듈입니다.
import * as functions from "firebase-functions";
//firebase-admin: Firebase 관리 기능을 사용하기 위한 모듈입니다.
import * as admin from "firebase-admin";
admin.initializeApp();
//Firebase Cloud Functions를 정의합니다.
//onVideoCreated 함수는 Firestore 데이터베이스의 "videos" 컬렉션에서 새로운 문서가 생성될 때 실행됩니다.
//이 함수는 onCreate 이벤트 리스너로 등록되어 있습니다.
//onCreate 이벤트 리스너는 두 개의 매개변수를 받습니다:
export const onVideoCreated = functions.firestore
.document("videos/{videoId}")
.onCreate(async (snapshot, context) => {
snapshot.ref.update({ hello: "from fuctions" });
});
//snapshot: 이벤트가 발생한 문서에 대한 스냅샷입니다.
//context: 함수의 실행 컨텍스트에 대한 정보를 포함하는 객체입니다.
//함수는 snapshot.ref.update({ hello: "from functions" }); 라인을 사용하여
//새로운 문서의 "hello" 필드를 "from functions"로 업데이트합니다.
//이 코드는 Firestore "videos" 컬렉션에 새로운 문서가 생성될 때마다
//"hello" 필드를 "from functions"로 업데이트하는 Firebase Cloud Function입니다.
배포하기.
firebase deploy --only functions 명령어로 배포하기
firebase deploy --only functions
배포된 모습 (Firebase 콘솔)
function 동작하는 모습
1. 영상 촬영 후 Firestore에 업로드하면 videos/videoId 라는 Collection과 Document가 생성됨
2. videos에 Document가 생성된 것을 감지한 function 의 onVideoCreated() 함수가 동작함
3. "hello: from functions" 라는 필드값이 Update됨
- cloud-functions 가 기본적으로 제공하는 시스템 패키지 목록:
https://cloud.google.com/functions/docs/reference/system-packages
System Packages Included in Cloud Functions | Cloud Functions Documentation | Google Cloud
Send feedback Stay organized with collections Save and categorize content based on your preferences. System Packages Included in Cloud Functions This page lists the Ubuntu packages included in the Cloud Functions environment. These packages are installed v
cloud.google.com
'앱 개발 > Flutter' 카테고리의 다른 글
55. [Flutter] Flutter에서 Firestore 쿼리 사용법 (0) | 2024.01.23 |
---|---|
54. [Flutter] Firebase functions 에 ffmpeg 패키지로 영상 썸네일 저장하기 (1) | 2024.01.09 |
52. [Flutter] Firebase Storage 이용하기 (파일 업로드) (1) | 2024.01.05 |
51. [Flutter] FireStore 연동하기(NoSQL DB) (0) | 2023.12.19 |
50. [Flutter] Firebase Authentication (깃허브) 인증 구현하기 (1) | 2023.12.07 |