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

Migrate to null safety #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 19 additions & 24 deletions lib/futuristic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,45 @@ class Futuristic<T> extends StatefulWidget {

/// Widget to display while the [Future] is executing.
/// If null, a [CircularProgressIndicator] will be displayed.
final WidgetBuilder busyBuilder;
final WidgetBuilder? busyBuilder;

/// Widget to display when the [Future] has completed with an error.
/// If null, [initialBuilder] will be displayed again.
/// The [Object] is the [Error] or [Exception] returned by the [Future].
/// Call [VoidCallback] to start executing the [Future] again.
final Widget Function(BuildContext, Object, VoidCallback) errorBuilder;
final Widget Function(BuildContext, Object, VoidCallback)? errorBuilder;

/// Widget to display when the [Future] has completed successfully.
/// If null, [initialBuilder] will be displayed again.
final Widget Function(BuildContext, T) dataBuilder;
final Widget Function(BuildContext, T)? dataBuilder;

/// Callback to invoke when the [Future] has completed successfully.
/// Will only be invoked once per [Future] execution.
final ValueChanged<T> onData;
final ValueChanged<T>? onData;

/// Callback to invoke when the [Future] has completed with an error.
/// Will only be invoked once per [Future] execution.
/// Call [VoidCallback] to start executing the [Future] again.
final Function(Object, VoidCallback) onError;
final Function(Object, VoidCallback)? onError;

const Futuristic({
Key key,
@required this.futureBuilder,
Key? key,
required this.futureBuilder,
this.autoStart = false,
this.initialBuilder,
required this.initialBuilder,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README and comment on line 16 should also be updated to reflect this change, or (better) initialBuilder should be nullable and optional to preserve the original behavior.

this.busyBuilder,
this.errorBuilder,
this.dataBuilder,
this.onData,
this.onError
}) : assert(futureBuilder != null),
assert(autoStart ^ (initialBuilder != null)),
super(key: key);
}) : super(key: key);

@override
_FuturisticState<T> createState() => _FuturisticState<T>();
}

class _FuturisticState<T> extends State<Futuristic<T>> {
Future<T> _future;
Future<T>? _future;

@override
void initState() {
Expand Down Expand Up @@ -89,29 +87,26 @@ class _FuturisticState<T> extends State<Futuristic<T>> {
}

Widget _handleInitial(BuildContext context) {
if (widget.initialBuilder != null) {
return widget.initialBuilder(context, _execute);
}
return _defaultWidget();
return widget.initialBuilder(context, _execute);
}

Widget _handleSnapshot(BuildContext context, AsyncSnapshot<T> snapshot) {
if (snapshot.hasError) {
return _handleError(context, snapshot.error);
return _handleError(context, snapshot.error as Object);
}
return _handleData(context, snapshot.data);
return _handleData(context, snapshot.data as T);
}

Widget _handleError(BuildContext context, Object error) {
if (widget.errorBuilder != null) {
return widget.errorBuilder(context, error, _execute);
return widget.errorBuilder!(context, error, _execute);
}
return _handleInitial(context);
}

Widget _handleData(BuildContext context, T data) {
if (widget.dataBuilder != null) {
return widget.dataBuilder(context, data);
return widget.dataBuilder!(context, data);
}
return _handleInitial(context);
}
Expand All @@ -120,25 +115,25 @@ class _FuturisticState<T> extends State<Futuristic<T>> {
if (widget.busyBuilder == null) {
return _defaultBusyWidget();
}
return widget.busyBuilder(context);
return widget.busyBuilder!(context);
}

void _execute() {
setState(() {
_future = widget.futureBuilder();
_future.then(_onData).catchError(_onError);
_future!.then(_onData).catchError(_onError);
});
}

void _onData(T data) async {
if (widget.onData != null && _isActive()) {
widget.onData(data);
widget.onData!(data);
}
}

void _onError(Object e) async {
if (widget.onError != null && _isActive()) {
widget.onError(e, _execute);
widget.onError!(e, _execute);
}
}

Expand Down
188 changes: 0 additions & 188 deletions pubspec.lock

This file was deleted.

8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ author: Martin Rybak <[email protected]>
homepage: https://github.com/vgventures/futuristic

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand All @@ -14,7 +14,11 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.9.0 # Static analysis rules
pedantic: ^1.11.0 # Static analysis rules

analyzer:
enable-experiment:
- non-nullabl

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down