cloud_firestore_database_wrapper 1.1.1 copy "cloud_firestore_database_wrapper: ^1.1.1" to clipboard
cloud_firestore_database_wrapper: ^1.1.1 copied to clipboard

A package that wraps cloud firestore. The idea is that future implementation will be database agnostic and a user can choose which one they want to use.

example/main.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_firestore_database_wrapper/src/firestore_data_source.dart';
import 'package:cloud_firestore_database_wrapper/util/firestore_parser.dart';
import '../test/generate_model.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import '../test/mock_data/mock_user.dart' as mockusers;
import '../test/test_models/user.dart';

Future<void> main() async {
  // Pass all uncaught errors to Crashlytics.
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(
    MyApp(),
  );
}

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  // The GlobalKey keeps track of the visible state of the list items
  // while they are being animated.
  final GlobalKey<AnimatedListState> listKey = GlobalKey<AnimatedListState>();

  // backing data
  List<User> _data = [];
  final path = 'users';
  final FirebaseFirestore finstance = FirebaseFirestore.instance;
  final FirestoreDataSource dataSource = FirestoreDataSource(
      FirestoreParser(generateModel), FirebaseFirestore.instance);
  num index = 0;

  @override
  Future<void> initState() async {
    for (var user in mockusers.users) {
      var ref = finstance.collection(path).doc(user['uid']);
      ref.set(user);
      index++;
    }
    _data = await dataSource.getCollectionwithParams<User>(path);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 300,
          child: AnimatedList(
            // Give the Animated list the global key
            key: listKey,
            initialItemCount: _data.length,
            // Similar to ListView itemBuilder, but AnimatedList has
            // an additional animation parameter.
            itemBuilder: (context, index, animation) {
              // Breaking the row widget out as a method so that we can
              // share it with the _removeSingleItem() method.
              var name = _data[index].name;
              if (name != null) {
                return _buildItem(name, animation);
              }
              return _buildItem("", animation);
            },
          ),
        ),
        ElevatedButton(
          child: Text('Insert item', style: TextStyle(fontSize: 20)),
          onPressed: () {
            _insertSingleItem();
          },
        ),
        ElevatedButton(
          child: Text('Remove item', style: TextStyle(fontSize: 20)),
          onPressed: () {
            _removeSingleItem();
          },
        )
      ],
    );
  }

  // This is the animated row with the Card.
  Widget _buildItem(String name, Animation<double> animation) {
    return SizeTransition(
      sizeFactor: animation,
      child: Card(
        child: ListTile(
          title: Text(
            name,
            style: TextStyle(fontSize: 20),
          ),
        ),
      ),
    );
  }

  void _insertSingleItem() {
    // Arbitrary location for demonstration purposes
    int insertIndex = 2;
    // Add the item to the data list.
    var date = DateTime.now();

    var json = {
      "date": date.toString(),
      "email": "example@example.com",
      "name": "John",
      "photoUrl": "url",
      "phoneNumber": "1234567890",
      "type": 3,
      "userType": [],
      "score": 100
    };
    var user = User(json);
    dataSource.addDocToCollection(path, user);
    _data.insert(insertIndex, user);
  }

  void _removeSingleItem() {
    int removeIndex = 2;
    // Remove item from data list but keep copy to give to the animation.
    User removedItem = _data.removeAt(removeIndex);
    var uid = removedItem.uid;

    dataSource.delete(path + "/" + uid);
    // This builder is just for showing the row while it is still
    // animating away. The item is already gone from the data list.
  }
}
2
likes
120
pub points
0%
popularity

Publisher

unverified uploader

A package that wraps cloud firestore. The idea is that future implementation will be database agnostic and a user can choose which one they want to use.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

cloud_firestore, fake_cloud_firestore, flutter

More

Packages that depend on cloud_firestore_database_wrapper