ferry_flutter_bloc 0.0.4-beta.1
ferry_flutter_bloc: ^0.0.4-beta.1 copied to clipboard

Flutter Android iOS Linux macOS web Windows

A new Flutter package.

example/lib/main.dart

import 'dart:io';

import 'package:example/bloc.dart';
import 'package:example/mutation.dart';
import 'package:example/search_query.dart';
import 'package:example/simple.dart';
import 'package:ferry/ferry.dart';
import 'package:ferry_hive_store/ferry_hive_store.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:gql_http_link/gql_http_link.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

Future<Client> initClient() async {
  await Hive.initFlutter();

  final box = await Hive.openBox("graphql");

  final store = HiveStore(box);

  final cache = Cache(store: store);

  final link = HttpLink('http://$host:9002/graphql');

  final client = Client(
    link: link,
    cache: cache,
  );

  return client;
}

Future<void> main() async {
  GetIt.I.registerSingleton<Client>(await initClient());

  runApp(MyApp());
}

String get host {
  if (Platform.isAndroid) {
    return '10.0.2.2';
  } else {
    return 'localhost';
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      routes: {
        'simple': (_) => Simple(),
        'bloc': (_) => Bloc(),
        'mutation': (_) => Mutation(),
        'search': (_) => SearchQuery(),
      },
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Ferry Bloc"),
      ),
      body: ListView(
        children: [
          ListTile(
            title: Text('Simple example'),
            subtitle: Text('native query example'),
            onTap: () => Navigator.of(context).pushNamed('simple'),
          ),
          Divider(),
          ListTile(
            title: Text('BLOC example'),
            subtitle: Text('query, fetchMore, refetch'),
            onTap: () => Navigator.of(context).pushNamed('bloc'),
          ),
          Divider(),
          ListTile(
            title: Text('Mutation BLOC example'),
            subtitle: Text('simple form + submit to call mutation'),
            onTap: () => Navigator.of(context).pushNamed('mutation'),
          ),
          Divider(),
          ListTile(
            title: Text('Search query'),
            onTap: () => Navigator.of(context).pushNamed('search'),
          ),
          Divider(),
        ],
      ),
    );
  }
}