Skip to content

Commit

Permalink
Upgrade SDK, Firebase, null_safety
Browse files Browse the repository at this point in the history
Upgraded dependencies to SDK >=2.12.0 and Firebase_core ^1.0.0 along with required transitive dependencies. Works with SDK 2.13.0-77.0.dev
  • Loading branch information
torstenek committed Mar 8, 2021
1 parent f718433 commit 0c6b87d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 386 deletions.
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

0 comments on commit 0c6b87d

Please sign in to comment.