-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub_stream_provider.dart
66 lines (60 loc) · 2.24 KB
/
sub_stream_provider.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:flutter_sub/flutter_sub.dart';
import 'package:flutter_sub_provider/src/sub_provider.dart';
import 'package:provider/provider.dart';
/// Listens to a [Stream] and exposes its content to `child` and descendants.
///
/// Its main use-case is to provide to a large number of a widget the content
/// of a [Stream], without caring about reacting to events.
/// A typical example would be to expose the battery level, or a Firebase query.
///
/// Trying to use [Stream] to replace [ChangeNotifier] is outside of the scope
/// of this class.
///
/// The [keys] property can be used to recreate the [Stream] when desired.
///
/// It is considered an error to pass a stream that can emit errors without
/// providing a `catchError` method.
///
/// `initialData` determines the value exposed until the [Stream] emits a value.
///
/// By default, [SubStreamProvider] considers that the [Stream] listened uses
/// immutable data. As such, it will not rebuild dependents if the previous and
/// the new value are `==`.
/// To change this behavior, pass a custom `updateShouldNotify`.
///
/// This is a wrapper around [StreamProvider] with added [SubValue.keys].
///
/// See also:
/// - [Stream], which is listened by [SubStreamProvider].
/// - [StreamController], to create a [Stream].
class SubStreamProvider<T> extends SubProvider0<Stream<T>> {
/// Listens to a [Stream] and exposes its content to `child` and descendants.
const SubStreamProvider({
required super.create,
required this.initialData,
super.keys,
this.catchError,
this.updateShouldNotify,
super.builder,
super.child,
});
/// The data provided before a value of the [Stream] has loaded.
final T initialData;
/// Called to handle errors that might occur in the [Stream].
final ErrorBuilder<T>? catchError;
/// {@macro provider.updateshouldnotify}
final UpdateShouldNotify<T>? updateShouldNotify;
@override
Widget buildWithValue(BuildContext context, Stream<T> value, Widget? child) {
return StreamProvider<T>.value(
value: value,
initialData: initialData,
updateShouldNotify: updateShouldNotify,
builder: builder,
catchError: catchError,
child: child,
);
}
}