fluq 0.0.2
fluq: ^0.0.2 copied to clipboard

A simple state management api based on streams with query and mutation handlers

fluq #

Simple state management api based on streams

Usage #

Wrap all your app, or the part of the app where you want to use fluq in a FluqProvider widget

import 'package:fluq/fluq.dart';

void main() {
  runApp(
    FluqProvider(
      child: MyApp(),
    ),
  );
}

Declare your queries extending QueryModel and setting up an unique query id and implementing the fetch method

import 'package:fluq/fluq.dart';

class MyQuery extends QueryModel {
  @override
  get key => "my-query";

  @override
  Future fetch() async {
    final result = await something();
    return result;
  }
}

Access the result reactively to state changes with QueryBuilder widget

// ...
    QueryBuilder(
      query: MyQuery(),
      builder: (context, state) {
        if (state is QueryLoading) {
          return CircularProgressIndicator();
        }
        if (state is QueryError) {
          return Text('${state.error}');
        }
        final result = state as QueryResult;
          return Text(json.encode(result.data));
      },
    ),
// ...

Or simply attach a listener to a query to handle events on state change with QueryListener widget

// ...
    QueryListener(
      query: MyQuery(),
      listener: (context, state) {
        if (state is QueryError) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [Text(' Error!!!')],
              ),
            ),
          );
        }
      },
      child: // ...
    ),
// ...

You can interact with query states or handle mutation events by accessing Fluq instance with providers directly in your handlers

// ...
  final fluq = Fluq.of(context);

  // update state
  fluq.setQueryState(MyQuery().key, QueryError('this is an error!'));

  // invalidate state and redo fetch for single or all queries
  fluq.invalidateQuery(MyQuery().key);
  fluq.invalidateAllQuery();
// ...

Or using Mutation widget

// ...
  Mutation(
    fetch: () => callSomething(),
    update: (fluq, result) {
      fluq.setQueryState(MyQuery().key, QueryResult(result));
    },
    onError: (err) => print(err),
    builder: (context, fetch) => TextButton(
      onPressed: fetch,
      child: Text('trigger update'),
    ),
  ),
// ...
0
likes
110
pub points
0%
popularity

A simple state management api based on streams with query and mutation handlers

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

matcecco@gmail.com

License

MIT (LICENSE)

Dependencies

flutter, provider, rxdart

More

Packages that depend on fluq