Coder Social home page Coder Social logo

arv / replicache-sdk-flutter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rocicorp/replicache-sdk-flutter

0.0 1.0 0.0 8.5 MB

Replicant SDK for Flutter

Home Page: https://replicant-sdk-flutter-seven-theta.now.sh

License: Other

Java 23.50% Objective-C 17.62% Ruby 3.37% Dart 44.51% Shell 11.00%

replicache-sdk-flutter's Introduction

Replicache Flutter SDK - Quickstart

Hi! This tutorial will walk you through integrating Replicache into your Flutter mobile app.

If you have any problems working through this, or just have questions, please join us on Slack. We'd be happy to help.

Note: This document assumes you already know what Replicache is, why you might need it, and broadly how it works. If that's not true, see the Replicache homepage for an overview, or the design document for a detailed deep-dive.

1. Get the SDK

Download the Replicache SDK, then unzip it:

curl -o replicache-sdk.tar.gz -L https://github.com/rocicorp/replicache/releases/latest/download/replicache-sdk.tar.gz
tar xvzf replicache-sdk.tar.gz

2. Start a new, empty Flutter app

flutter create todo

3. Add the replicache dependency to your Flutter app's pubspec.yaml

...

  cupertino_icons: ^0.1.2

+   replicache:
+     path:
+       /path/to/replicache-sdk/flutter/

...

4. Instantiate Replicache

In main.dart, import the Replicache library:

import 'package:replicache/replicache.dart';

Then replace the generated _MyHomePageState class with:

class _MyHomePageState extends State<MyHomePage> {
  Replicache _rep = Replicache(
    'http://localhost:7001');

  @override
  Widget build(BuildContext context) {
    return Text("Hello");
  }
}

Now launch the Flutter app:

cd todo
flutter emulators --launch apple_ios_simulator
flutter run

You will see Replicache start up and start trying to sync, but fail because no diff-server is running. That's OK! We'll fix that in the next step.

For now, search for ClientID in the output and copy it down. Every device syncing with Replicache has a unique ClientID generated at first run. We'll need that value next.

5. Start a development diff-server and put some sample data in it:

Under normal circumstances, Replicache periodically pulls a snapshot of user data that should be persistent on the client (the Client View) from your service. Replicache computes a diff for each client and sends only the changes as part of downstream sync.

You will need set up integration with your service later (see server-side integration).

But while you're working on the client side, it's easiest to just inject snapshots directly from the command line.

In a new tab, start a development diffs server and leave it running:

/path/to/replicache-sdk/<platform>/diffs --db=/tmp/foo serve --enable-inject

Then, in a third tab, inject a snapshot into the diff server:

CLIENT_ID=<your-client-id-from-step-4>
curl -d @- http://localhost:7001/inject << EOF
{
  "accountID": "sandbox",
  "clientID": "$CLIENT_ID",
  "clientViewResponse": {
    "clientView": {
      "/list/29597": {
        "id": 29597,
        "ownerUserID": 3
      },
      "/todo/14136": {
        "complete": false,
        "id": 14136,
        "listId": 29597,
        "order": 0.5,
        "text": "Take out the trash"
      },
      "lastTransactionID":"0"
    }
  }
}
EOF

Notes:

  • To get the clientID value search the log output of the Flutter app for ClientID. Replicache prints it out early in startup.
  • The accountID is your unique account ID on diff-server. During our early alpha testing, use "sandbox".
  • You'll setup lastTransactionID later in this tutorial. For now just return 0.

6. Read Data

Replace the _MyHomePageState class with:

class _MyHomePageState extends State<MyHomePage> {
  List<Map<String, dynamic>> _events = [];
  Replicache _replicache = new Replicache('http://localhost:7001');

  _MyHomePageState() {
    _replicache.onChange = _handleChange;
    _handleChange();
  }
  
  void _handleChange() async {
    var events = List<Map<String, dynamic>>.from(
      (await _replicache.scan(prefix: '/todo/')).map((item) => item.value));
    setState(() {
      _events = events;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: List.from(
            _events.map(
              (Map m) => CheckboxListTile(value: m['complete'], title: Text(m['text'])))),
        ),
      ),
    );
  }
}

7. Update Data

Now inject a new snapshot, you'll see your view dynamically update:

curl -d @- http://localhost:7001/inject << EOF
{
  "accountID": "sandbox",
  "clientID": "$CLIENT_ID",
  "clientViewResponse": {
    "clientView": {
      "/list/29597": {
        "id": 29597,
        "ownerUserID": 3
      },
      "/todo/14136": {
        "complete": true,
        "id": 14136,
        "listId": 29597,
        "order": 0.5,
        "text": "Take out the trash"
      },
      "/todo/9081": {
        "complete": false,
        "id": 9081,
        "listId": 29597,
        "order": 0.75,
        "text": "Walk the dog"
      },
      "lastTransactionID":"0"
    }
  }
}
EOF

You will see the Flutter app update and display a new TODO and check off the previous one. Nice!

8. Write Data

TODO (this isn't implemented in the SDK yet)

Congratulations โ€” you are done with the client setup ๐ŸŽ‰. Time for a cup of coffee.

In fact, while you're away, why not turn off the wifi and click around. Your app will respond instantly with cached data and queue up the changes to replay, once you setup the server-side integration.

Next steps

More questions?

replicache-sdk-flutter's People

Contributors

aboodman avatar ahl avatar arv avatar elsigh avatar jdunck avatar lesnitsky avatar ripventura avatar sboodman avatar

Watchers

 avatar

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.