The-ring-io / flutter_phoenix

Easily restart your application from scratch.
MIT License
153 stars 17 forks source link

Feature request: provide a way to execute some code while restarting it. #27

Open arguswaikhom opened 1 year ago

arguswaikhom commented 1 year ago

We can let the app developer execute code everytime it's restarting the app.

POC

class _PhoenixState  extends State<MainWidget> {
  Key key = UniqueKey();

  void restartApp() => setState(() => key = UniqueKey());

  Future<void> _initializeFlutterFire() async {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );

    await FirebaseCrashlytics.instance
        .setCrashlyticsCollectionEnabled(!kDebugMode);

    FlutterError.onError = (errorDetails) {
      FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
    };

    // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
    PlatformDispatcher.instance.onError = (error, stack) {
      FirebaseCrashlytics.instance.recordError(error, stack);
      return true;
    };
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initializeFlutterFire(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return KeyedSubtree(key: key, child: widget.child);
        } else {
          return const SizedBox();
        }
      },
    );
  }
}

_initializeFlutterFire method in the example should be a method developers can pass while creating the Phoenix widget.