firebase_storage_hooks 0.0.1+14 copy "firebase_storage_hooks: ^0.0.1+14" to clipboard
firebase_storage_hooks: ^0.0.1+14 copied to clipboard

discontinuedreplaced by: riverpod

Hooks used for common firebase storage operations such as selecting and uploading file(s)

example/lib/main.dart

import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';
import 'package:firebase_storage_hooks/firebase_storage_hooks.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.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: 'Flutter Demo',
      theme: ThemeData.dark(),
      home: const ImagePage(),
    );
  }
}

class ImagePage extends HookWidget {
  const ImagePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final fileHandler = useFile(fileType: FileType.image);

    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await fileHandler.select();
        },
      ),
      body: Builder(
        builder: (context) {
          if (fileHandler.snapshot.hasError) {
            return Center(
              child: Text(fileHandler.snapshot.error.toString()),
            );
          } else if (fileHandler.snapshot.hasData) {
            final file = fileHandler.snapshot.data!;

            if (kIsWeb) {
              return Image.memory(file.bytes!);
            } else {
              return Image.file(File(file.path!));
            }
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
}

class FirebaseImagePage extends HookWidget {
  const FirebaseImagePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final storageFileHandler = useStorageFile(
      fileType: FileType.image,
      reference: FirebaseStorage.instance.ref(),
      fileNameBuilder: (fileName) => fileName.replaceAll(' ', '_'),
    );

    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          await storageFileHandler.fileHandler.select();
        },
      ),
      body: Builder(
        builder: (context) {
          final fileSnapshot = storageFileHandler.fileHandler.snapshot;

          if (fileSnapshot.hasError) {
            return Center(
              child: Text(fileSnapshot.error.toString()),
            );
          } else if (fileSnapshot.hasData) {
            final file = fileSnapshot.data!;

            final image = (kIsWeb
                ? MemoryImage(file.bytes!)
                : FileImage(File(file.path!))) as ImageProvider;

            return Ink.image(
              image: image,
              child: InkWell(
                onTap: () async {
                  final commitHandler = await storageFileHandler.maybeCommit();

                  if (commitHandler == null) return;

                  ScaffoldMessenger.of(context).showSnackBar(
                    SnackBar(
                      content: Text(
                        'Committed image to ${commitHandler.value}',
                      ),
                      action: SnackBarAction(
                        onPressed: commitHandler.revert,
                        label: 'Undo',
                      ),
                    ),
                  );
                },
              ),
            );
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }
}
1
likes
100
pub points
0%
popularity

Publisher

unverified uploader

Hooks used for common firebase storage operations such as selecting and uploading file(s)

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

file_picker, firebase_hooks_core, firebase_storage, flutter, flutter_hooks, path

More

Packages that depend on firebase_storage_hooks