bloc_annotations 0.6.0

bloc_annotations #

This package is part of a number of packages designed to help create BLoC architected code in Dart and Flutter. You can view them all in the repository here.

This README relies on the fact that you have read the main README.

Installation #

dependencies:
  bloc_annotations: <latest_version>

Input Class #

All the annotated members of your BLoC class that have annotations must be public as the generated code uses them. Members without annotations can still be used normally such as in @BLoCMapper methods. @BLoCValues initial data will will be added to the stream once all service have been initialized.

Different Controllers #

Using StreamController isn't required so rxdarts subjects such as BehaviourSubject can be used as shown in the example.

Output Class #

The output class can be seen in the generated .bloc.dart file. The BLoCs name will be the same as the template class with the suffix of BLoC.

Importing the Output Class #

The generated .bloc.dart file uses a part of statment, so your template file must contain a part statement.

Changelog #

0.6.0 #

  • Require services to be initialized by the template
  • Allow BLoCValue to have initial data
  • Replace individual service require with generic
  • Move BLoCParamater to the constructor
  • Overhaul bloc_generator

0.5.1 #

  • Add BLoCMultiProvider and BLoCMultiDisposer

0.5.0 #

  • Add Documentation
  • Refactor
  • Format
  • Add CI
  • Make parameters private by default

0.4.4 #

  • Changed name from flutter_bloc_generator to bloc_generator

0.3.0 #

  • Removed AsyncMapperService
  • Made all mappers streams
  • Split out providers and disposer to flutter_bloc_provider package

0.2.6 #

  • Add MapperService, AsyncMapperService, RequireMapperService and RequireAsyncMapperService

0.2.5 #

  • Currect trigger types to Future
  • Make mappers optionally async

0.2.4 #

  • Add TriggeredService

0.2.3 #

  • Allow mappers to return null to not add anything to the output

0.2.2 #

  • Added parameters that can be passed to the BLoC and are accessible to BLoC services

0.2.1 #

  • Make requiring a service require the service type
  • Add BLoCService service that takes an entire BLoC instead of a Stream or Sink

0.2.0 #

  • Make mappers async

0.1.9 #

  • Make template value the only current value stored

0.1.8 #

  • Moved value updater before calling mappers
  • Updating latest values on template

0.1.7 #

  • OutputService automatic listener
  • OutputService automatic subscription disposer

0.1.6 #

  • Remove current data from mappers

0.1.5 #

  • Made services public members

0.1.4 #

  • Changed Service to InputService and added OutputService

0.1.3 #

  • Format code
  • Add example

0.1.2 #

  • Fix latest value updater for initial data

0.1.1 #

  • Change analyzer dependency to ^0.33.0 for compatability

0.1.0 #

  • Initial public release

example/lib/main.dart

// Copyright 2019 Callum Iddon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'package:flutter/material.dart';

import 'package:flutter_bloc_provider/flutter_bloc_provider.dart';

import 'package:flutter_bloc_example/bloc.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(final BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BLoCDisposer<TestBLoC>(
        bloc: TestBLoC(maxValue: 20),
        child: const MyHomePage(title: 'Flutter bloc_generator Demo'),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  const MyHomePage({this.title});

  @override
  Widget build(final BuildContext context) {
    final TestBLoC bloc = BLoCProvider.of<TestBLoC>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            StreamBuilder<String>(
              stream: bloc.counter,
              builder: (final BuildContext context,
                  final AsyncSnapshot<String> snapshot) {
                if (!snapshot.hasData) {
                  return Container();
                }

                return Column(children: <Widget>[
                  Text(
                    snapshot.data,
                    style: Theme.of(context).textTheme.display1,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      MaterialButton(
                        child: const Text('Reset Counter'),
                        onPressed: () => bloc.setCounter.add(0),
                      ),
                      MaterialButton(
                        child: const Text('Trigger Url Service'),
                        onPressed: () {
                          if (int.parse(bloc.currentCounter) != 0) {
                            bloc.urlService.trigger();
                          } else {
                            print('Hit the + button first.');
                          }
                        },
                      ),
                    ],
                  ),
                ]);
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => bloc.addToCounter.add(1),
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  bloc_annotations: ^0.6.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:bloc_annotations/bloc_annotations.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
27
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Sep 10, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:bloc_annotations/bloc_annotations.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
meta ^1.1.6 1.1.7