Storage & File Uploads
Flint Dart includes a Storage
utility to easily store, update, and delete files. This is useful for profile pictures, document uploads, or any user-generated files.
Storing Files
Use Storage.create()
to store a new file. You can optionally specify a subdirectory where the file should be saved.
import 'package:flint_dart/storage.dart';
// Store a file in the default storage location
final url = await Storage.create(file);
// Store a file in a subdirectory
final url = await Storage.create(file, subdirectory: 'profiles');
Updating Files
Use Storage.update()
to replace an existing file with a new one. Pass the old file's URL/path and the new file object.
// Update an existing profile picture
final updatedUrl = await Storage.update(
oldFileUrl,
file,
subdirectory: 'profiles',
);
Deleting Files
Use Storage.delete()
to remove a file from storage.
// Delete a file
await Storage.delete(fileUrl);
Handling File Uploads in Controllers
You can check if a file exists in the request with req.hasFile()
and retrieve it using req.file()
.
import 'package:flint_dart/flint_dart.dart';
import 'package:flint_dart/storage.dart';
import 'package:sample/src/models/user_model.dart';
class UserController {
Future<void> updateProfilePic(Request req, Response res) async {
final String userId = req.params['id']!;
final User? user = await User().find(userId);
if (await req.hasFile('profile_pic')) {
final file = await req.file('profile_pic');
if (file != null) {
String profilePicUrl;
if (user != null && user.profilePicUrl != null) {
// Update existing file
profilePicUrl = await Storage.update(
user.profilePicUrl!,
file,
subdirectory: 'profiles',
);
} else {
// Create new file
profilePicUrl = await Storage.create(
file,
subdirectory: 'profiles',
);
}
await user!.update(userId, {'profile_pic': profilePicUrl});
res.json({
"status": "success",
"message": "Profile picture updated.",
"url": profilePicUrl,
});
return;
}
}
res.status(400).json({
"status": "error",
"message": "No profile picture uploaded.",
});
}
}
File Paths & URLs
By default, Flint Dart stores files in your configured storage path (e.g., storage/
). You can serve these files publicly using your static file middleware or CDN.