firebase_cached_image 0.2.0 firebase_cached_image: ^0.2.0 copied to clipboard
Cache Manager and Cached ImageProvider for Firebase Cloud Storage Objects.
import 'dart:typed_data';
import 'package:firebase_cached_image/firebase_cached_image.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase Cached Image',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Cached Image Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Image(
image: FirebaseImageProvider(
firebaseUrl: "gs://bucket_f233/logo.jpg",
options: CacheOptions(
source: Source.server,
),
),
frameBuilder: (_, child, frame, __) {
if (frame == null) return child;
return const CircularProgressIndicator();
},
),
),
);
}
Future<Uint8List?> getDocument() async {
await FirebaseCachedImage.initialise();
FirebaseCachedImage.instance.cacheOptions = CacheOptions(
shouldCache: false,
);
//if both ref and firebaseUrl are passed then ref will be used.
return await FirebaseCachedImage.instance.get(
firebaseUrl: "gs://bucket_f233/doc.pdf",
// optional
options: CacheOptions(
source: Source.cacheServerByMetadata,
metadataRefreshInBackground: false,
),
// Can also pass ref
ref: FirebaseStorage.instance.ref().child("doc.pdf"),
);
}
}