Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): document and remove deprecated api methods #10

Merged
merged 2 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ addons:
- ubuntu-toolchain-r-test # you need this source to get the right version of libstdc++6
packages:
- libstdc++6
- fonts-droid
- fonts-droid-fallback
install:
- echo 'Avoid default Travis CI install step' # this is to avoid an error with pub in Travis
before_script:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [1.1.0] - 2021-01-04

- Fix deprecated api usage.
- Update documentation.

## [1.0.9] - 2019-04-26

- Connect BlocProvider's updateShouldNotifyOverride with its state.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The `BlocProvider` class depends on `InheritedWidget`. Whenever you want to get

In order to control this behavior, the static method `of` has an optional boolean argument (which is true by default) which determines wether your context will be attached or not.

Basically, if you don't provide it or you just set it to `true`, [inheritFromWidgetOfExactType](https://docs.flutter.io/flutter/widgets/BuildContext/inheritFromWidgetOfExactType.html) will be used. If you set it to `false` then [ancestorInheritedElementForWidgetOfExactType](https://docs.flutter.io/flutter/widgets/BuildContext/ancestorInheritedElementForWidgetOfExactType.html) will be used instead.
Basically, if you don't provide it or you just set it to `true`, [dependOnInheritedWidgetOfExactType](https://docs.flutter.io/flutter/widgets/BuildContext/dependOnInheritedWidgetOfExactType.html) will be used. If you set it to `false` then [getElementForInheritedWidgetOfExactType](https://docs.flutter.io/flutter/widgets/BuildContext/getElementForInheritedWidgetOfExactType.html) will be used instead.

## Customizing the update policy

Expand Down
22 changes: 17 additions & 5 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ class MainPage extends StatelessWidget {
final appBloc = BlocProvider.of<AppBloc>(context);
return Scaffold(
body: SafeArea(
child: StreamBuilder(
stream: appBloc.text$,
builder: (context, AsyncSnapshot<String> snapshot) {
return Text(snapshot.hasData ? snapshot.data : 'loading');
},
child: Column(
children: [
StreamBuilder(
stream: appBloc.text$,
builder: (context, AsyncSnapshot<String> snapshot) {
return Text(
snapshot.hasData ? snapshot.data ?? 'no data' : 'loading');
},
),
FlatButton(
onPressed: () {
var time = DateTime.now().toUtc().toIso8601String();
appBloc.text.add('Pressed at $time');
},
child: Text('PRESS THIS'),
)
],
),
),
);
Expand Down
3 changes: 2 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: example

environment:
sdk: '>=2.0.0-dev.68.0 <3.0.0'
sdk: '>=2.7.0 <3.0.0'
flutter: ">=1.17.0"

dependencies:
flutter:
Expand Down
56 changes: 51 additions & 5 deletions lib/src/bloc_provider.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
import 'package:flutter/material.dart';
import 'bloc.dart';

/// Function used to customize the update policy
typedef UpdateShouldNotify<T> = bool Function(T bloc, _BlocProvider oldWidget);

/// The `BlocProvider` class depends on `InheritedWidget`.
/// It accepts a bloc and a widget.
class BlocProvider<T extends Bloc> extends StatefulWidget {
/// The BLoC this provider will be hosting
final T bloc;

/// The widget that the [BlocProvider] will wrap
final Widget child;

/// Allows you to override the default update policy
///
/// Default implementation:
///
/// ```dart
/// @override
/// bool updateShouldNotify(_BlocProvider oldWidget) =>
/// updateShouldNotifyOverride != null
/// ? updateShouldNotifyOverride(bloc, oldWidget)
/// : oldWidget.bloc != bloc;
/// ```
final UpdateShouldNotify<T> updateShouldNotifyOverride;

/// Builds a [BlocProvider].
///
/// [child] is the widget that the [BlocProvider] will wrap.
/// [bloc] is the BLoC this provider will be hosting.
/// [updateShouldNotifiyOverride] is an optional parameter that will allow you
/// to override the default behaviour.
/// This is the default implementation of the `updateShouldNotify` method:
///
/// ```dart
/// @override
/// bool updateShouldNotify(_BlocProvider oldWidget) =>
/// updateShouldNotifyOverride != null
/// ? updateShouldNotifyOverride(bloc, oldWidget)
/// : oldWidget.bloc != bloc;
/// ```
BlocProvider({
Key key,
@required this.child,
@required this.bloc,
this.updateShouldNotifyOverride,
}) : super(key: key);

////// Whenever you want to get your `BloC`, you can decide wether to attach the context of your widget to the `InheritedWidget` or not.
/// In order to control this behavior, the static method `of` has an optional boolean argument (which is true by default) which determines wether your context will be attached or not.
/// Basically, if you don't provide it or you just set it to `true`, [dependOnInheritedWidgetOfExactType](https://api.flutter.dev/flutter/widgets/BuildContext/dependOnInheritedWidgetOfExactType.html) will be used.
/// If you set it to `false` then [getElementForInheritedWidgetOfExactType](https://api.flutter.dev/flutter/widgets/BuildContext/getElementForInheritedWidgetOfExactType.html) will be used instead.
static T of<T extends Bloc>(BuildContext context,
[bool attachContext = true]) =>
_BlocProvider.of(context, attachContext);

/// Creates the state
@override
_BlocProviderState<T> createState() => _BlocProviderState<T>();
}
Expand Down Expand Up @@ -52,13 +90,21 @@ class _BlocProvider<T extends Bloc> extends InheritedWidget {
}) : super(child: child);

static T of<T extends Bloc>(BuildContext context, bool attachContext) {
final type = _typeOf<_BlocProvider<T>>();
final blocProvider = attachContext
? (context.inheritFromWidgetOfExactType(type) as _BlocProvider)
: (context.ancestorInheritedElementForWidgetOfExactType(type).widget
as _BlocProvider);
_BlocProvider<T> blocProvider;

if (attachContext) {
blocProvider =
context.dependOnInheritedWidgetOfExactType<_BlocProvider<T>>();
} else {
var element =
context.getElementForInheritedWidgetOfExactType<_BlocProvider<T>>();
if (element != null) {
blocProvider = element.widget as _BlocProvider<T>;
}
}

if (blocProvider == null) {
final type = _typeOf<_BlocProvider<T>>();
throw FlutterError('Unable to find BLoC of type $type.\n'
'Context provided: $context');
}
Expand Down
70 changes: 39 additions & 31 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.5.0-nullsafety.1"
boolean_selector:
dependency: transitive
description:
name: boolean_selector
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "2.1.0-nullsafety.1"
characters:
dependency: transitive
description:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.3"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.2"
version: "1.2.0-nullsafety.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.14.11"
version: "1.15.0-nullsafety.3"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0-nullsafety.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -45,35 +66,21 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.5"
version: "0.12.10-nullsafety.1"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.3.0-nullsafety.3"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.6.2"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "1.8.0-nullsafety.1"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -85,55 +92,56 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.5"
version: "1.8.0-nullsafety.2"
stack_trace:
dependency: transitive
description:
name: stack_trace
url: "https://pub.dartlang.org"
source: hosted
version: "1.9.3"
version: "1.10.0-nullsafety.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0-nullsafety.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.4"
version: "1.1.0-nullsafety.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0-nullsafety.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.4"
version: "0.2.19-nullsafety.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.6"
version: "1.3.0-nullsafety.3"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.8"
version: "2.1.0-nullsafety.3"
sdks:
dart: ">=2.2.0 <3.0.0"
dart: ">=2.10.0-110 <2.11.0"
flutter: ">=1.17.0"
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: generic_bloc_provider
description: A generic BloC Provider for your Flutter apps. This package will help you avoid the boilerplate of writing BloC Providers.
version: 1.0.9
author: Roberto Huertas <roberto.huertas@outlook.com>
version: 1.1.0
homepage: https://github.com/robertohuertasm/generic_bloc_provider
repository: https://github.com/robertohuertasm/generic_bloc_provider
issue_tracker: https://github.com/robertohuertasm/generic_bloc_provider/issues

environment:
sdk: '>=2.0.0-dev.68.0 <3.0.0'
sdk: '>=2.7.0 <3.0.0'
flutter: ">=1.17.0"

dependencies:
flutter:
Expand Down