Code With Explain X Example NBR
Code With Explain X Example NBR
✅
CRUD stands for:
✅
Create → Add new data
✅
Read → Get data
✅
Update → Change existing data
Delete → Remove data
BR
Imagine we are making a contact list app where we can add, view, update, and delete
people’s information. To do this, we use a database called SQLite in Flutter.
dependencies:
sqflite: any
path_provider: any
N
Then, run this command to install them:
Code:
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
return openDatabase(
path, BR
version: 1,
onCreate: (db, version) {
db.execute('''
CREATE TABLE contacts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT
)
''');
},
);
}
N
Explanation:
Code:
Future<void> insertContact(Database db, String name, String phone) async {
await db.insert(
'contacts',
{'name': name, 'phone': phone},
}
);
Explanation:
BR
conflictAlgorithm: ConflictAlgorithm.replace,
✅ Example Use:
N
await insertContact(db, "Alice", "12345");
Result:
id Name Phon
e
1 Alice 12345
2 Bob 67890
B. READ (Query Data)
Code:
Future<List<Map<String, dynamic>>> getContacts(Database db) async {
return await db.query('contacts');
}
Explanation:
✅ Output:
[
{id: 1, name: Alice, phone: 12345},
{id: 2, name: Bob, phone: 67890}
]This shows all saved contacts.
N
C. UPDATE (Modify Data)
Code:
Future<void> updateContact(Database db, String name, String newPhone) async {
await db.update(
'contacts',
{'phone': newPhone},
where: 'name = ?',
whereArgs: [name],
);
}
Explanation:
✅ Example Use:
await updateContact(db, "Alice", "54321");
Updated Database:
id
2
Name
Alice
Bob
BR
Phon
e
54321
67890
Code:
N
Future<void> deleteContact(Database db, String name) async {
await db.delete(
'contacts',
where: 'name = ?',
whereArgs: [name],
);
}
Explanation:
✅ Final Database:
id Name Phon
e
2 Bob 67890
Summary
Operation
Create
Read
BR Purpose
Insert new
data
✅
Fast → Works quickly
✅
Lightweight → Uses less memory
Good for offline storage → Saves data on your phone, even without the internet
Imagine Hive as a box where you can put and get things using a label (key).
dependencies:
hive: any
hive_flutter: any
Code:
var box = await Hive.openBox('myBox');
Explanation:
Code:
BR
box.put('name', 'Alice');
Explanation:
✅ Example Use:
N
await box.put('name', 'Alice');
Code:
var name = box.get('name');
print(name); // Output: Alice
Explanation:
Code:
BR
box.put('name', 'Bob');
Explanation:
✅ Example Use:
N
await box.put('name', 'Bob');
Code:
box.delete('name');
Explanation:
✅ Example Use:
await box.delete('name');
Summary BR
Operation Purpose Example Code
Imagine ObjectBox as a storage system where you store objects (like your User object) instead
of just data in tables. It's super fast and works well for apps on mobile devices.
dependencies:
objectbox: any
Code:
var box = store.box<User>();
Explanation:
🔹 box() → Creates or opens a User box where we will store user data (like name and age).
Now, you have a place where you can store your user data as objects (not just rows of data).
Step 3: Performing CRUD Operations
A. CREATE (Insert Data)
Code:
box.put(User(name: 'Alice', age: 25));
Explanation:
🔹 put(User(name: 'Alice', age: 25)) → Adds a new User object with the name "Alice" and
BR
🔹 This will save the User as an object in the database, not just text or numbers.
age 25 to the box.
✅ Example Use:
await box.put(User(name: 'Alice', age: 25));
Code:
N
var user = box.get(1); // Get user by ID
print(user?.name); // Output: Alice
Explanation:
✅ Example Output:
Alice
C. UPDATE (Modify Data)
Code:
var user = box.get(1);
if (user != null) {
user.age = 26; // Update age
box.put(user); // Save updated user
}
Explanation:
Code:
box.remove(1); // Remove user by ID
Explanation:
Summary
Operation Purpose Example Code
Delete
BR Remove data by ID await box.remove(1);
N
What is Firebase Firestore?
Firebase Firestore is a cloud-based NoSQL database that stores data and syncs it in
real-time. It’s perfect for apps with multiple users because data changes automatically across all
devices in real-time.
BR
1️⃣ Add Firebase to your Flutter project (via Firebase Console). 2️⃣ Install the Firestore
package. Open your pubspec.yaml file and add the following dependency:
dependencies:
cloud_firestore: ^4.0.0
Code:
FirebaseFirestore.instance
.collection('users')
.add({'name': 'Alice', 'age': 25});
Explanation:
🔹 collection('users') → Refers to the users collection in Firestore where the data will be
🔹 add({'name': 'Alice', 'age': 25}) → Adds a new document with the name "Alice" and age
saved.
✅ Example Use:
await FirebaseFirestore.instance.collection('users').add({
'name': 'Alice',
'age': 25,
});
BR
B. READ (Query Data)
Code:
var snapshot = await FirebaseFirestore.instance.collection('users').get();
snapshot.docs.forEach((doc) {
print(doc.data()); // Output: Map of user data
});
N
Explanation:
✅ Example Output:
{name: Alice, age: 25}
{name: Bob, age: 30}
Explanation:
Code:
FirebaseFirestore.instance
.collection('users')
.doc('documentId') // The document ID you want to delete
.delete();
N
Explanation:
Update
Delete
BR
Modify
data
await
FirebaseFirestore.instance.collection('users').doc('us
erID123').update({'age': 26});
Remov await
e data FirebaseFirestore.instance.collection('users').doc('us
erID123').delete();
N
What is a REST API?
A REST API (Representational State Transfer) is a way for your app to communicate with an
external server using HTTP methods (POST, GET, PUT, DELETE). Data is usually transferred in
a format like JSON.
BR
1️⃣ Open your pubspec.yaml file and add the following dependency:
dependencies:
http: ^0.15.0
Sending data to the server to create a new resource (like a new user).
Code:
Uri.parse('https://example.com/api/users'),
Explanation:
Uri.parse('https://example.com/api/users'),
BR
body: {'name': 'Alice', 'age': '25'},
);
Code:
N
var response = await http.get(Uri.parse('https://example.com/api/users'));
Explanation:
✅ Example Use:
await http.get(Uri.parse('https://example.com/api/users'));
C. UPDATE (PUT/PATCH Request)
Code:
Uri.parse('https://example.com/api/users/1'),
);
Explanation:
BR
🔹 http.put() → Sends a PUT request to update an existing resource.
🔹 Uri.parse('https://example.com/api/users/1') → Specifies the server URL with the ID of
🔹 body: {'name': 'Alice', 'age': '26'} → Sends the updated data for that user.
the user you want to update.
✅ Example Use:
await http.put(
N
Uri.parse('https://example.com/api/users/1'),
);
Code:
Uri.parse('https://example.com/api/users/1'),
);
Explanation:
✅ Example Use:
await http.delete(Uri.parse('https://example.com/api/users/1'));
Database
SQLite
BR Create
db.insert()
Read
db.query()
Update
db.update()
Delete
db.delete()
- TANVIR NIBIR