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
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(),
);
}
}
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};
}
}
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!'),
),
);
}
}
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');
}
}
}
-
Jalankan Aplikasi Pastikan semuanya berjalan tanpa error. Jika Firebase belum terhubung, cek koneksi dan file
firebase_options.dart
. -
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'), ), ), ); } }
-
Periksa Firestore
- Pastikan data pasien dummy tersimpan di Firestore.
- Jika semuanya berjalan lancar, kita lanjutkan ke fitur pendaftaran pasien. 🚀