Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade SDK, Firebase, null_safety #32

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
.pub-cache/
.pub/
build/
pubspec.lock

# Android related
**/android/**/gradle-wrapper.jar
Expand Down
6 changes: 4 additions & 2 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class MyApp extends StatelessWidget {
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
MyHomePage({ Key? key, required this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}



class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
Expand All @@ -42,7 +44,7 @@ class _MyHomePageState extends State<MyHomePage> {
'gs://bucket123/userIcon123.jpg',
shouldCache: true, // The image should be cached (default: True)
maxSizeBytes: 3000 * 1000, // 3MB max file size (default: 2.5MB)
cacheRefreshStrategy: CacheRefreshStrategy.NEVER // Switch off update checking
cacheRefreshStrategy: CacheRefreshStrategy.NEVER,
),
width: 100,
),
Expand Down
26 changes: 12 additions & 14 deletions lib/src/cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import 'package:path/path.dart';
class FirebaseImageCacheManager {
static const String key = 'firebase_image';

Database db;
late Database db;
static const String dbName = '$key.db';
static const String table = 'images';
String basePath;
late String basePath;

final CacheRefreshStrategy cacheRefreshStrategy;

Expand All @@ -25,8 +25,9 @@ class FirebaseImageCacheManager {
);

Future<void> open() async {
var dbPath = (await getDatabasesPath())!;
db = await openDatabase(
join(await getDatabasesPath(), dbName),
join(dbPath, dbName),
onCreate: (Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
Expand All @@ -44,7 +45,7 @@ class FirebaseImageCacheManager {
}

Future<FirebaseImageObject> insert(FirebaseImageObject model) async {
await db.insert(table, model.toMap());
await db.insert('images', model.toMap());
return model;
}

Expand Down Expand Up @@ -76,7 +77,7 @@ class FirebaseImageCacheManager {
return maps.length > 0;
}

Future<FirebaseImageObject> get(String uri, FirebaseImage image) async {
Future<FirebaseImageObject?> get(String uri, FirebaseImage image) async {
final List<Map<String, dynamic>> maps = await db.query(
table,
columns: const [
Expand All @@ -100,7 +101,7 @@ class FirebaseImageCacheManager {
return null;
}

Reference getImageRef(FirebaseImageObject object, FirebaseApp firebaseApp) {
Reference getImageRef(FirebaseImageObject object, FirebaseApp? firebaseApp) {
FirebaseStorage storage =
FirebaseStorage.instanceFor(app: firebaseApp, bucket: object.bucket);
return storage.ref().child(object.remotePath);
Expand All @@ -109,7 +110,7 @@ class FirebaseImageCacheManager {
Future<void> checkForUpdate(
FirebaseImageObject object, FirebaseImage image) async {
int remoteVersion =
(await object.reference.getMetadata()).updated.millisecondsSinceEpoch;
(await object.reference.getMetadata()).updated!.millisecondsSinceEpoch;
if (remoteVersion != object.version) {
// If true, download new image for next load
await this.upsertRemoteFileToCache(object, image.maxSizeBytes);
Expand All @@ -131,14 +132,14 @@ class FirebaseImageCacheManager {
);
}

Future<Uint8List> localFileBytes(FirebaseImageObject object) async {
Future<Uint8List?> localFileBytes(FirebaseImageObject object) async {
if (await _fileExists(object)) {
return File(object.localPath).readAsBytes();
}
return null;
}

Future<Uint8List> remoteFileBytes(
Future<Uint8List?> remoteFileBytes(
FirebaseImageObject object, int maxSizeBytes) {
return object.reference.getData(maxSizeBytes);
}
Expand All @@ -147,9 +148,9 @@ class FirebaseImageCacheManager {
FirebaseImageObject object, int maxSizeBytes) async {
if (CacheRefreshStrategy.BY_METADATA_DATE == this.cacheRefreshStrategy) {
object.version =
(await object.reference.getMetadata()).updated.millisecondsSinceEpoch;
(await object.reference.getMetadata()).updated!.millisecondsSinceEpoch;
}
Uint8List bytes = await remoteFileBytes(object, maxSizeBytes);
Uint8List bytes = (await remoteFileBytes(object, maxSizeBytes))!;
await putFile(object, bytes);
return bytes;
}
Expand All @@ -166,9 +167,6 @@ class FirebaseImageCacheManager {
}

Future<bool> _fileExists(FirebaseImageObject object) async {
if (object?.localPath == null) {
return false;
}
return File(object.localPath).exists();
}

Expand Down
13 changes: 7 additions & 6 deletions lib/src/firebase_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
final CacheRefreshStrategy cacheRefreshStrategy;

/// Default: the default Firebase app. Specifies a custom Firebase app to make the request to the bucket from (optional)
final FirebaseApp firebaseApp;
FirebaseApp? firebaseApp = Firebase.app();

/// The model for the image object
final FirebaseImageObject _imageObject;
Expand All @@ -47,6 +47,7 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
bucket: _getBucket(location),
remotePath: _getImagePath(location),
reference: _getImageRef(location, firebaseApp),
localPath: '',
);

/// Returns the image as bytes
Expand All @@ -64,21 +65,21 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
return uri.path;
}

static Reference _getImageRef(String location, FirebaseApp firebaseApp) {
static Reference _getImageRef(String location, FirebaseApp? firebaseApp) {
FirebaseStorage storage = FirebaseStorage.instanceFor(
app: firebaseApp, bucket: _getBucket(location));
return storage.ref().child(_getImagePath(location));
}

Future<Uint8List> _fetchImage() async {
Uint8List bytes;
Uint8List? bytes;
FirebaseImageCacheManager cacheManager = FirebaseImageCacheManager(
cacheRefreshStrategy,
);

if (shouldCache) {
await cacheManager.open();
FirebaseImageObject localObject =
FirebaseImageObject? localObject =
await cacheManager.get(_imageObject.uri, this);

if (localObject != null) {
Expand All @@ -96,11 +97,11 @@ class FirebaseImage extends ImageProvider<FirebaseImage> {
await cacheManager.remoteFileBytes(_imageObject, this.maxSizeBytes);
}

return bytes;
return bytes!;
}

Future<Codec> _fetchImageCodec() async {
return await PaintingBinding.instance
return await PaintingBinding.instance!
.instantiateImageCodec(await _fetchImage());
}

Expand Down
8 changes: 4 additions & 4 deletions lib/src/image_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class FirebaseImageObject {

FirebaseImageObject({
this.version = -1,
this.reference,
this.localPath,
this.bucket,
this.remotePath,
required this.reference,
required this.localPath,
required this.bucket,
required this.remotePath,
}) : uri = '$bucket$remotePath';

Map<String, dynamic> toMap() {
Expand Down
Loading