Giter VIP home page Giter VIP logo

flutterway / firebase_for_all Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 5.0 97 KB

This package is the way to use firebase features on all platforms without code changes. This package will deal with channelizing task of these packages for you. It will automatically switch between packages and allow you to run "Firedart" on Windows and "cloud_firestore" on any other platform.

License: MIT License

Kotlin 0.18% Swift 0.55% Objective-C 0.05% Dart 99.21%
firebase firebase-authentication firebase-storage firestore

firebase_for_all's Introduction

This package is the way to use firebase features on all platforms without code changes. Its developed by using these packages(For desktop part):

firebase_dart & firedart are both great packages that will suffice all your needs for firebase. On non-windows patforms it is more desirable to use original package with minimum risk of bugs and problems.

This package will deal with channelizing task of these packages for you. It will automatically switch between packages and allow you to run "Firedart" on Windows and "cloud_firestore" on any other platform.

New classes and functions have been designed carefully so that you dont have to modify your old codes and migrate your project very easily.

Like everyone else, I'm looking forward to the end of the flutterFire desktop process. I hope this library can help a little bit until then

Features

Currently supports the following firebase services:

  • Authentication
  • Cloud firestore
  • Cloud storage

Examples

All examples in example/lib/functions.dart

Usage

The documentation link will be added in future

You must add the library as a dependency to your project.

dependencies:
 firebase_for_all: ^latest

If you setup your Firebase via FlutterFire CLI then InitializeApp function will throw an error. Do this to avoid this error:

dependency_overrides:
  firebase_core_platform_interface: 4.5.1

Then run flutter packages get

Getting started

Initiliaze firebase and choose the features you want. There are two ways to configure firebase for your project.

  • Old school way: configure everything by hand
  • New flutterfire way by using firebase CLI

Old school way:

  await FirebaseCoreForAll.initializeApp(
      options: FirebaseOptions(
        apiKey: 'XXXXXXXXXXXXXXXXXXXXXX',
        appId: 'XXXXXXXXXXXXXXXXXXXXXX',
        messagingSenderId: 'XXXXXXXXXXX',
        projectId: 'XXXXXXXXXXXXXX',
        authDomain: 'XXXXXXXXXXXXX.firebaseapp.com',
        storageBucket: 'XXXXXXXXXXXXX.appspot.com',
      ),
      firestore: true,
      auth: true,
      storage: true);

New flutterfire way:

  await FirebaseCoreForAll.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
      firestore: true,
      auth: true,
      storage: true);

Cloud Storage

get FirebaseAuth.instance with "FirebaseAuthForAll.instance"

    await FirebaseAuthForAll.instance.createUserWithEmailAndPassword(email: "[email protected]", password: "password123");

Cloud Firestore

Document & Query Snapshots

QuerySnapshot

FirestoreForAll.instance
.collection('users')
.get()
.then((QuerySnapshotForAll querySnapshot) {
    querySnapshot.docs.forEach((doc) {
        print(doc["first_name"]);
    });
});

DocumentSnapshot

FirestoreForAll.instance
    .collection('users')
    .doc(userId)
    .get()
    .then((DocumentSnapshotForAll documentSnapshot) {
      if (documentSnapshot.exists) {
        print('Document data: ${documentSnapshot.data()}');
      } else {
        print('Document does not exist on the database');
      }
    });

Querying

Filtering

Warning: "isNotEqualTo" and "whereNotIn" dont work on desktop(except MACOS). You should use it with this in mind

FirestoreForAll.instance
    .collection('users')
    .where('age', isGreaterThan: 10)
    .get()
    .then(...);

Ordering

FirestoreForAll.instance
    .collection('users')
    .orderBy('age', descending: true)
    .get()
    .then(...);

Limiting

Warning: "limitToLast" doesnt work on desktop(except MACOS). You should use it with this in mind

FirestoreForAll.instance
    .collection('users')
    .limit(2)
    .get()
    .then(...);

Start & End Cursors

Warning: Unfortunately this feature doesnt work on desktop(except MACOS)

FirestoreForAll.instance
    .collection('users')
    .orderBy('age')
    .orderBy('company')
    .startAt([4, 'Alphabet Inc.'])
    .endAt([21, 'Google LLC'])
    .get()
    .then(...);

Adding&Updating&Deleting Documents

ColRef users = FirestoreForAll.instance.collection('users');
users
    .add({
      'full_name': fullName, // John Doe
      'company': company, // Stokes and Sons
      'age': age // 42
    })
    .then((value) => print("User Added"))
    .catchError((error) => print("Failed to add user: $error"));

users
    .doc("12345")
    .set({
      'full_name': fullName, // John Doe
      'company': company, // Stokes and Sons
      'age': age // 42
    })
    .then((value) => print("User Added"))
    .catchError((error) => print("Failed to add user: $error"));

users
    .doc('ABC123')
    .update({'company': 'Stokes and Sons'})
    .then((value) => print("User Updated"))
    .catchError((error) => print("Failed to update user: $error"));

users
    .doc('ABC123')
    .delete()
    .then((value) => print("User Deleted"))
    .catchError((error) => print("Failed to delete user: $error"));

Realtime changes

Listening collection

CollectionSnapshots  collectionSnapshots  = FirestoreForAll.instance.collection('users').snapshots();
collectionSnapshots.listen(
  (snapshot) {},
  onDone: () {},
  onError: (e, stackTrace) {},
);

Warning: StreamBuilder doesnt work for this one. Use this instead:

CollectionBuilder(
        stream: FirestoreForAll.instance.collection("users").snapshots(),
        builder: (BuildContext context,
            AsyncSnapshot<QuerySnapshotForAll> snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Text("Loading");
          }

          return ListView(
            children:
                snapshot.data!.docs.map((DocumentSnapshotForAll document) {
              Map<String, dynamic> data =
                  document.data() as Map<String, dynamic>;
              return ListTile(
                title: Text(data['name']),
                subtitle: Text(data['surname']),
              );
            }).toList(),
          );
        },
      )

Listening document

DocumentSnapshots documentSnapshots=FirestoreForAll.instance.collection('users').doc('ABC123').snapshots();
documentSnapshots.listen(
  (snapshot) {},
  onDone: () {},
  onError: (e, stackTrace) {},
);

Warning: StreamBuilder doesnt work for this one. Use this instead:

DocumentBuilder(
        stream: FirestoreForAll.instance
            .collection("users")
            .doc('ABC123')
            .snapshots(),
        builder: (BuildContext context,
            AsyncSnapshot<DocumentSnapshotForAll> snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return Text("Loading");
          }

          return ListTile(
            title: Text(snapshot.data!['name']),
            subtitle: Text(snapshot.data!['surname']),
          );
        },
      )

Typing CollectionReference and DocumentReference

The only thing that has changed here is "User.fromJson(snapshot.map!)" instead of "User.fromJson(snapshot.data()!)"

final moviesRef = FirestoreForAll.instance.collection('movies').withConverter<User>(
      fromFirestore: (snapshot, _) => User.fromJson(snapshot.map!),
      toFirestore: (movie, _) => movie.toJson(),
    );
// Obtain 31 aged users
  List<DocumentSnapshotForAll<User>> users = await userRef
      .where('age', isEqualTo: 31)
      .get()
      .then((snapshot) => snapshot.docs);

  // Add a user
  await userRef.add(
    User(
      name: 'Chris',
      surname: 'Doe',
      age: 20
    ),
  );

  // Get a user with the id 42
  User user = await userRef.doc('42').get().then((snapshot) => snapshot.data()!);

Cloud Storage

Upload Files

Upload from a file

  final storageRef = FirebaseStorageForAll.instance.ref();
  final mountainsRef = storageRef.child("mountains.jpg");
  Directory appDocDir = await getApplicationDocumentsDirectory();
  String filePath = '${appDocDir.absolute}/file-to-upload.png';
  File file = File(filePath);

  UploadTaskForAll task= mountainsRef.putFile(file);

Upload from a String

  String dataUrl = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==';
  UploadTaskForAll task= mountainsRef.putString(dataUrl, format: PutStringFormat.dataUrl);

Uploading raw data

  UploadTaskForAll  task= mountainsRef.putData(file.readAsBytesSync());

Get Download Url

  await mountainsRef.getDownloadURL();

Manage Uploads

    File file = File(filePath);
  mountainsRef.putFile(file).snapshotEvents.listen((taskSnapshot) {
  switch (taskSnapshot.state) {
    case TaskState.running:
      // ...
      break;
    case TaskState.paused:
      // ...
      break;
    case TaskState.success:
      // ...
      break;
    case TaskState.canceled:
      // ...
      break;
    case TaskState.error:
      // ...
      break;
  }
});

Download Files

Download in memory

  final storageRef = FirebaseStorageForAll.instance.ref();
  final islandRef = storageRef.child("images/island.jpg");
  const oneMegabyte = 1024 * 1024;
  final Uint8List? data = await islandRef.getData(oneMegabyte);

Download to a local file

  final islandRef = storageRef.child("images/island.jpg");

  final appDocDir = await getApplicationDocumentsDirectory();
  final filePath = "${appDocDir.absolute}/images/island.jpg";
  final file = File(filePath);

  DownloadTaskForAll downloadTask = await islandRef.writeToFile(file);

Manage Downloads

  downloadTask.snapshotEvents.listen((taskSnapshot) {
    switch (taskSnapshot.state) {
      case TaskState.running:
        // TODO: Handle this case.
        break;
      case TaskState.paused:
        // TODO: Handle this case.
        break;
      case TaskState.success:
        // TODO: Handle this case.
        break;
      case TaskState.canceled:
        // TODO: Handle this case.
        break;
      case TaskState.error:
        // TODO: Handle this case.
        break;
    }
  });

Delete File

  final desertRef = storageRef.child("images/desert.jpg");
  await desertRef.delete();

List all files

final storageRef = FirebaseStorage.instance.ref().child("files/uid");
final listResult = await storageRef.listAll();
for (var prefix in listResult.prefixes) {
  // The prefixes under storageRef.
  // You can call listAll() recursively on them.
}
for (var item in listResult.items) {
  // The items under storageRef.
}

New Features

StorageFile

class StorageFile{
  String cloudPath;
  String fileName;
  StorageRef reference;
  String type,extension;
  List<String> relatedDirs;
  int size;
}

StorageDirectory

class StorageDirectory{
  String cloudPath;
  String dirName;
  StorageRef reference;
  List<String> relatedDirs;
}

New Function - getFiles()

This function is to get information of every files inside reference path

List<StorageFile> files = await storageRef.getFiles();

New Function - getSize()

This function is to get the file size

  final desertRef = FirebaseStorageForAll.instance.ref().child("images/desert.jpg");

  int size = await desertRef.getSize();

New Function - getDirectories()

This function is to get information of every directories inside reference path

  List<StorageDirectory> files = await storageRef.getDirectories();

New Function - getDirSize()

This function is to get the file size

  final storageRef = FirebaseStorageForAll.instance.ref().child("files/uid");

  int size = await storageRef.getDirSize();

firebase_for_all's People

Contributors

worldwidee avatar

Stargazers

Pinglei Guo avatar  avatar ke chankrisna avatar Aymen Denoub avatar Pownthep Laokhunthot avatar

Watchers

João Moreira avatar

firebase_for_all's Issues

CollectionBuilder doesn't verify if collection exists

If I want to use CollectionBuilder from a docId collection the connectionState remains "Waiting" even if the collection doesn't exist.

Loading infinitely and after a while trigger the error

Stream terminated...

erro stream terminated

Error to run in Desktop

When I try to run it gives me this error:

C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: El comando "setlocal [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: C:\Windows\System32\nuget.exe install Microsoft.Windows.ImplementationLibrary -Version 1.0.211019.2 -ExcludeVersion -OutputDirectory E:/projectname/simple_mine_project_app/build/windows/packages [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: C:\Windows\System32\nuget.exe install Microsoft.Web.WebView2 -Version 1.0.1108.44 -ExcludeVersion -OutputDirectory E:/projectname/build/windows/packages [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: if %errorlevel% neq 0 goto :cmEnd [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: :cmEnd [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: :cmErrorLevel [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: exit /b %1 [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: :cmDone [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: if %errorlevel% neq 0 goto :VCEnd [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(150,5): error MSB3073: :VCEnd" sali� con el c�digo 1. [E:\projectname\build\windows\plugins\desktop_webview_auth\desktop_webview_auth_DEPENDENCIES_DOWNLOAD.vcxproj]
Exception: Build process failed.

Unable to run base example

Launching lib\main.dart on sdk gphone x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/firebase_core-1.24.0/lib/src/firebase_app.dart:18:25: Error: Member not found: 'FirebaseAppPlatform.verifyExtends'.
    FirebaseAppPlatform.verifyExtends(_delegate);
                        ^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/firebase_auth_platform_interface-6.10.1/lib/src/action_code_info.dart:65:15: Error: The method 'FallThroughError' isn't defined for the class 'ActionCodeInfo'.
 - 'ActionCodeInfo' is from 'package:firebase_auth_platform_interface/src/action_code_info.dart' ('/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/firebase_auth_platform_interface-6.10.1/lib/src/action_code_info.dart').
Try correcting the name to the name of an existing method, or defining a method named 'FallThroughError'.
        throw FallThroughError();
              ^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:17:3: Error: Type 'BidirectionalIterator' not found.
  BidirectionalIterator<V> fromIterator(V anchor,
  ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:21:3: Error: Type 'BidirectionalIterator' not found.
  BidirectionalIterator<V> get iterator;
  ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:23:3: Error: Type 'BidirectionalIterator' not found.
  BidirectionalIterator<V> get reverseIterator;
  ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:184:3: Error: Type 'BidirectionalIterator' not found.
  BidirectionalIterator<V> get iterator => TreeIterator(this);
  ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:187:3: Error: Type 'BidirectionalIterator' not found.
  BidirectionalIterator<V> get reverseIterator =>
  ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:305:3: Error: Type 'BidirectionalIterator' not found.
  BidirectionalIterator<V> fromIterator(V anchor,
  ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:527:31: Error: Type 'BidirectionalIterator' not found.
class TreeIterator<V> extends BidirectionalIterator<V> {
                              ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:579:29: Error: Type 'BidirectionalIterator' not found.
class TreeCursor<V> extends BidirectionalIterator<V> {
                            ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/sortedmap-0.5.2/lib/src/treeset.dart:254:36: Error: 'BidirectionalIterator' isn't a type.
        i1 = hasMore1 ? i1 : i2 as BidirectionalIterator<V>;
                                   ^^^^^^^^^^^^^^^^^^^^^
/C:/Users/<name>/AppData/Local/Pub/Cache/hosted/pub.dev/cloud_firestore_platform_interface-5.7.7/lib/src/platform_interface/utils/load_bundle_task_state.dart:13:13: Error: Method not found: 'FallThroughError'.
      throw FallThroughError();
            ^^^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception


FAILURE: Build failed with an exception.

* Where:
Script 'C:\tools\flutter\packages\flutter_tools\gradle\src\main\groovy\flutter.groovy' line: 1350

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\tools\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 4s
Exception: Gradle task assembleDebug failed with exit code 1

Authentication for storage

Thx for putting this together.
But I am not sure how to use authentication for Storage on Windows!
I dont see it is working!

Error in Firestore Windows auth

First of all thank you very much for this package.

This is an error of unauthorized when firestore is set with allow read, write: if request.auth != null;.

Works on ios and Android but not in Windows.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.