Skip to content

Latest commit

 

History

History
145 lines (122 loc) · 3.79 KB

002.1.APP_APOTEK_FLUTTERFIRE.md

File metadata and controls

145 lines (122 loc) · 3.79 KB

DI USAHAKAN SUDAH MENGIKUTI LANGKAH CONNECT FLUTTER DENGAN FIREBASE

1. Struktur Folder Awal

Berikut adalah struktur folder dasar yang akan kita gunakan:

lib/
├── main.dart                # Entry point aplikasi
├── firebase_options.dart    # File konfigurasi Firebase otomatis
├── models/                  # Folder untuk model data
├── screens/                 # Folder untuk layar aplikasi
└── services/                # Folder untuk fungsi terkait Firebase

2. File Dasar

main.dart

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'screens/home_screen.dart';
import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Poliklinik App',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: HomeScreen(),
    );
  }
}

3. Placeholder untuk Folder Utama

models/

Buat model untuk pasien. File: models/patient.dart:

class Patient {
  final String id;
  final String nama;
  final String alamat;

  Patient({required this.id, required this.nama, required this.alamat});

  Map<String, dynamic> toMap() {
    return {'id': id, 'nama': nama, 'alamat': alamat};
  }
}

screens/

Buat layar dasar untuk Home. File: screens/home_screen.dart:

import 'package:flutter/material.dart';

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Poliklinik App')),
      body: Center(
        child: Text('Selamat Datang di Poliklinik App!'),
      ),
    );
  }
}

services/

File: services/firebase_service.dart. Untuk logika Firebase sederhana:

import 'package:cloud_firestore/cloud_firestore.dart';

class FirebaseService {
  final CollectionReference _patients =
      FirebaseFirestore.instance.collection('patients');

  Future<void> addPatient(Map<String, dynamic> patientData) async {
    try {
      await _patients.add(patientData);
      print('Pasien berhasil ditambahkan.');
    } catch (e) {
      print('Error: $e');
    }
  }
}

Langkah Setelahnya

  1. Jalankan Aplikasi Pastikan semuanya berjalan tanpa error. Jika Firebase belum terhubung, cek koneksi dan file firebase_options.dart.

  2. Tes Firebase Tambahkan tombol di HomeScreen untuk menyimpan data dummy ke Firestore:

    import 'package:flutter/material.dart';
    import '../services/firebase_service.dart';
    
    class HomeScreen extends StatelessWidget {
      final FirebaseService _firebaseService = FirebaseService();
    
      void _testAddPatient() {
        _firebaseService.addPatient({
          'id': '001',
          'nama': 'John Doe',
          'alamat': 'Jl. Merdeka No. 1',
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: Text('Poliklinik App')),
          body: Center(
            child: ElevatedButton(
              onPressed: _testAddPatient,
              child: Text('Tambah Pasien Dummy'),
            ),
          ),
        );
      }
    }
  3. Periksa Firestore

    • Pastikan data pasien dummy tersimpan di Firestore.

Next Step

  • Jika semuanya berjalan lancar, kita lanjutkan ke fitur pendaftaran pasien. 🚀