123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- import 'dart:async';
- import 'dart:ui' as ui;
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:fis_lib_qrcode/qr_flutter.dart';
- import 'package:fis_ui/index.dart';
- class CustomFutureBuilder<T> extends StatefulWidget implements FWidget {
-
-
-
- const CustomFutureBuilder({
- Key? key,
- this.future,
- this.initialData,
- required this.builder,
- }) : assert(builder != null),
- super(key: key);
- final Future<T>? future;
- final AsyncWidgetBuilder<T> builder;
- final T? initialData;
- static bool debugRethrowError = false;
- @override
- State<CustomFutureBuilder<T>> createState() => _CustomFutureBuilderState<T>();
- }
- class _CustomFutureBuilderState<T> extends State<CustomFutureBuilder<T>> {
-
-
-
- Object? _activeCallbackIdentity;
- late AsyncSnapshot<T> _snapshot;
- @override
- void initState() {
- super.initState();
- _snapshot = widget.initialData == null
- ? AsyncSnapshot<T>.nothing()
- : AsyncSnapshot<T>.withData(
- ConnectionState.none, widget.initialData as T);
- _subscribe();
- }
- @override
- void didUpdateWidget(CustomFutureBuilder<T> oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (oldWidget.future != widget.future) {
- if (_activeCallbackIdentity != null) {
- _unsubscribe();
- _snapshot = _snapshot.inState(ConnectionState.none);
- }
- _subscribe();
- }
- }
- @override
- Widget build(BuildContext context) => widget.builder(context, _snapshot);
- @override
- void dispose() {
- _unsubscribe();
- super.dispose();
- }
- void _subscribe() {
- if (widget.future != null) {
- final Object callbackIdentity = Object();
- _activeCallbackIdentity = callbackIdentity;
- widget.future!.then<void>((T data) {
- if (_activeCallbackIdentity == callbackIdentity) {
- setState(() {
- _snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data);
- });
- }
- }, onError: (Object error, StackTrace stackTrace) {
- if (_activeCallbackIdentity == callbackIdentity) {
- setState(() {
- _snapshot = AsyncSnapshot<T>.withError(
- ConnectionState.done, error, stackTrace);
- });
- }
- assert(() {
- if (FutureBuilder.debugRethrowError) {
- Future<Object>.error(error, stackTrace);
- }
- return true;
- }());
- });
- _snapshot = _snapshot.inState(ConnectionState.waiting);
- }
- }
- void _unsubscribe() {
- _activeCallbackIdentity = null;
- }
- }
|