firefly 0.1.1 copy "firefly: ^0.1.1" to clipboard
firefly: ^0.1.1 copied to clipboard

outdated

A lightweight Flutter Widget for interacting with your Firestore database.

Firefly Package Logo

A lightweight Flutter widget that allows for short and clean widget trees when interacting with data from your Firestore database.

Usage #

There is three steps to start using Firefly.

Setup Firebase #

We recommend following the FlutterFire guide to setup your app with Firebase.

It´s important to setup Firebase before using any Firefly widgets.

We like to use a FutureBuilder to initialize Firebase.

Widget build(BuildContext context) {
    final Future<FirebaseApp> _initialization = Firebase.initializeApp();

    return FutureBuilder(
      // Initialize FlutterFire:
      future: _initialization,
      builder: (context, snapshot) {
        // Check for errors
        if (snapshot.hasError) {
          return Text('ERROR');
        }

        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) {
          return FireflyApp();
        }

        // Otherwise, show something whilst waiting for initialization to complete
        return Text('LOAD');
      },
    );
  }

Use the FireflyProvider #

The FireflyProvider will provide all Firefly widgets with the correct instance of your Firestore database. It also needs a list of FireflyDataBuilder. The purpose of the FireflyDataBuilder is to provide Firefly with the correct model and constuctor so it can create your objects before passing them back.

class FireflyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
  
    final firestore = FirebaseFirestore.instance;
    
    final buildList = [
      FireflyDataBuilder(
        model: Person,
        builder: (json) => Person.fromJson(json),
      )
    ];
    
    return FireflyProvider(
      instance: firestore,
      modelbuilderList: buildList,
      child: App(),
    );
  }
}

Finally, you can use Firefly #

Firefly requires two things to work correctly. The collection parameter is used so Firefly knows which Firestore Collection to retrive data from. The type of model expected to returned in the builder. Here we expect a Person by adding <Person>

We can then use the builder to get a list of created Person:s, here described as state.

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Firefly<Person>(
          collection: 'persons',
          builder: (context, state) => ListView.builder(
            itemCount: state.length,
            itemBuilder: (context, index) => Text(state[index].name),
          ),
        ),
      ),
    );
  }
}

Using the Firefly widget #

The Firefly widget has a bunch of parameters:

listBuilder #

The builder is one way to use retrive the created objects. But you can also use the shorthand listBuilder to get a already created ListView with objects.

You can´t combine many builders, so remember to only use one per Firefly widget.

Firefly<Person>(
  collection: 'persons',
  listBuilder: (context, state, index) => Text(state[index].name),
),

Querying #

You can also query the Firestore directly on the widget. Use the query parameter and the Query model.

Just make sure you hide the Firestore Query in your import with:

import 'package:cloud_firestore/cloud_firestore.dart' hide Query;

Firefly<Person>(
  collection: 'persons',
  listBuilder: (context, state, index) => Text(state[index].name),
  query: Query('age')..isEqualTo(22),
),

Override Loading #

Want to show another widget when the Firestore is loading. Just override the loading parameter with any widget you like.

Firefly<Person>(
  collection: 'players',
  listBuilder: (context, state, index) => Text(state[index].name),
  loading: Container(
    width: 200,
    height: 200,
    color: Colors.red,
  ),
),

Handle Errors #

If you get an error reciving your data, handle it with the error parameter.

Firefly<Person>(
  collection: 'players',
  listBuilder: (context, state, index) => Text(state[index].name),
  error: (error) => Text(error.toString()),
),
2
likes
0
pub points
0%
popularity

Publisher

verified publisheralster.se

A lightweight Flutter Widget for interacting with your Firestore database.

Homepage
Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

cloud_firestore, flutter, provider

More

Packages that depend on firefly