Using Firestore Storage and Caching Files inside of Dart's Flutter Framework

in #utopian-io6 years ago

flutter-logo.jpg

Repository

https://github.com/flutter/flutter

What Will I Learn?

  • You will learn how to cache files in Flutter
  • You will learn about the Asset Bundle
  • You will learn how to upload files to Firebase Storage
  • You will learn how to download files from Firebase Storage
  • You will learn how to add and remove files from the Asset Bundle
  • You will learn how to make Regular Expressions

Requirements

System Requirements:
OS Support for Flutter:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • A little understanding of caching and storage
  • A fair understanding of Mobile development and Imperative or Object Oriented Programming
  • Some understanding of file explorers

Resources for Flutter and this Project:

Sources:

Flutter Logo (Google): https://flutter.io/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Flutter Tutorial Video, we build a very simple application that allows us to upload photos to Firebase Storage. The user is then able to download the files back to the application and then serve them through the Image.asset() widget. We make use of Regular Expressions and file caching through the asset bundle object. We also read and write data to these temporary files as byte streams.

Outline for this Tutorial
Firebase Storage in Flutter

Firebase and Cloud Firestore provide a way of storing user made data in a real-time cloud database. While we can store URIs and URLs into these databases, we have no ability to directly upload a more complex fileformat like a video or image file. This is what Firebase Storage is for. As a scaling, media database, Firebase Storage gives the developer the ability to store any type of data in an intuitive bucket storage system.

storage-fb.png

As you can see from the console, the basic storage module is very similar to Firebase and it can be used in a way that is also very similar to a typical Firebase or Cloud Firestore database. Each file is stored in a bucket which works like a document or key/value pair from the other two databases. You are then able to give your application's the ability to download and upload data from these buckets at will.

The Flutter Asset Bundle

Flutter allows the developer to interact with external files and web files through its asset bundle. The asset bundle changes as we move up and down the widget tree based on what pieces of data are exposed to that node on the tree. The root asset bundle is made out of two bundle types, a network asset bundle and a default asset bundle. As the name implies, the network asset bundle handles the network assets and the default asset bundle handles the local assets.

upload-file.png

In the uploadFile function, we pull our pictures from the local storage folders through the root asset bundle and convert them into a stream of bytes. We are then able to create a duplicate file in the operating system's temporary folder. We are able to upload this file to the Firebase Storage bucket and we can also use this file to cache the original asset from the root bundle.

Filtering through URLs and File names with a Regular Expression

In this application, we make use of a regular expression inside of the downloadFile function.

regex-dart.png

This RegEx allows us to quickly filter through the download URL path to pick out the simple filename of our target file. Regular Expressions are very useful for filtering and searching through various types of strings and can be used in many different situations.

regex.png

The Regular Expression that we use in this application is made up of two primary capturing groups. The first group starts with a ^ character which indicates that we want to negate this pattern. The pattern just looks for the literal characters ? and /. By negating these characters we can filter out any part of the string which contains these characters. At the end of this capture group, [^?/] is an asterisks which signifies that we want to match none of these proceeding tokens. Following this asterisks, we have an escaped period \.. This period specifies that we want a string that contains a period which bridges the first capture group and the second capture group. The final capture group looks for the three characters jpg in that order. In this way we are able to specifically grab the filename for our file from the download URL.

The source code for this project can be found here

Regexpr Website can be found here

Video Tutorial

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Minesweeper Game

Weather Application

Curriculum

Proof of Work Done

https://github.com/tensor-programming

Sort:  

Great work @tensor.

Your contribution has been evaluated according to Utopian rules and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post,Click here


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

That was very fast. Thank you again for moderating my contribution.

Yeah, we work at 'flight' speed at Utopian. Very efficient eh? :)

That made me chuckle. Yes, very efficient hahaha

Hey @tensor
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64344.02
ETH 3142.36
USDT 1.00
SBD 4.01