Flutter & Firebase CRUD
Catatan : Coding Talk Udacoding
Minggu, 27 Oktober 2019
Mengaktifkan Auntetification
Pilih Authentication > Sign In Method > Google
Membuat App
Create Project Flutter
Project : Flutter_Crud_Firestore
Mendapatkan Package
Buka android > app > build.gradle
Medapatkan SHA1
keytool -list -v -alias androiddebugkey -keystore C:\Users\maulana\.android\debug.keystore
Password : android
SHA1 lewat AS
Klik Menu Tools > Flutter > Open In Android Studio
Pilih Gradle sebelah kiri > android > Task > android > signingReport
Download google-service.json
copy ke folder android > app
Tambah Depedencies
buildscript {
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
//tambahan
classpath 'com.google.gms:google-services:4.3.2'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
android/build.gradle
android/app/build.gradle
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 28
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.flutter_crud_firestore"
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
def multidex_version "2.0.1"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//tambahan
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.android.support:$multidex_version'
}
//tambahan
apply plugin: 'com.google.gms.google-services'
buat folder
folder image & font
pubspec.yaml
name: flutter_crud_firestore
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
firebase_auth: ^0.14.0+5
google_sign_in: ^4.0.7
cloud_firestore: ^0.12.9+5
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- image/
fonts:
- family: Pacifico
fonts:
- asset: font/Pacifico-Regular.ttf
main.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import './view/list_catatan.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Firebase CRUD',
theme: ThemeData(
primarySwatch: Colors.red,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = new GoogleSignIn();
Future<FirebaseAuth> _signIn() async {
GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken);
FirebaseUser firebaseUser =
(await firebaseAuth.signInWithCredential(credential)).user;
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) =>
CatatanQu(user: firebaseUser, googleSignIn : googleSignIn)));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
width: double.infinity,
decoration: BoxDecoration(color: Colors.redAccent),
child: Column(
children: <Widget>[
new Padding(
padding: EdgeInsets.only(top: 110),
),
new Icon(
Icons.event_note,
color: Colors.white,
size: 150.0,
),
new Text(
"CatatanQu",
style: TextStyle(
fontSize: 48.0, fontFamily: 'Pacifico', color: Colors.white),
),
new Padding(padding: EdgeInsets.only(bottom: 110.0)),
new Padding(
padding: EdgeInsets.only(top: 110),
),
Padding(
padding: const EdgeInsets.all(32),
child: MaterialButton(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Image.asset(
"image/google3.png",
width: 50,
),
Text(
"Sign in with Google",
style: TextStyle(fontSize: 19.0, color: Colors.grey),
textAlign: TextAlign.start,
)
],
),
color: Colors.white,
minWidth: 300.0,
height: 55.0,
elevation: 16.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(32.0),
side: BorderSide(color: Colors.white)),
onPressed: () {
_signIn();
},
),
)
],
),
),
);
}
}
add_catatan.dart
comming soon
edit_catatan.dart
comming soon
list_catatan.dart
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
class CatatanQu extends StatefulWidget {
CatatanQu({this.user, this.googleSignIn});
final FirebaseUser user;
final GoogleSignIn googleSignIn;
@override
_CatatanQuState createState() => _CatatanQuState();
}
class _CatatanQuState extends State<CatatanQu> {
@override
Widget build(BuildContext context) {
return Container(
);
}
}
Flutter & Firestore
By Maulana Ilham
Flutter & Firestore
- 736