Lucas Knötig
Lucas Knötig

Reputation: 122

Server controlled Maintenance Screen in Flutter (Firebase as Backend)

So i have a question... is it possible, that I can implement a maintenance screen to my app when I need it?

So like, that the App is checking status from the server and when the Value is at maintenance for example, the App loads to the maintenance screen. And when the Value is changed, the App is starting into the normal main screen.

Idk if i can do this with remote config...

import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'package:showcaseview/showcaseview.dart';

import '../providers/user-provider.dart';
import '../screens/auth_screen.dart';
import '../screens/startscreen.dart';

class AutoLoginHandler extends StatefulWidget {
  @override
  State<AutoLoginHandler> createState() => _AutoLoginHandlerState();
}

class _AutoLoginHandlerState extends State<AutoLoginHandler> {
  @override
  Widget build(BuildContext context) {
    UserProvider up = context.read<UserProvider>();
    return StreamBuilder<User?>(
      //Streambuilder looks if data is avaliable
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (context, snapshot) {
        if (snapshot.hasData && snapshot.data != null) {
          up.setUser(snapshot.data);
          return ShowCaseWidget(
            builder: Builder(builder: (context) => MainPage()),
            
          ); //when data here goto Startscreen
        }
        return LoginScreen(); //when no data is here goto Login
      },
    );
  }
}

The code attached is the Code that checks, if the User is registered.

Upvotes: 1

Views: 1120

Answers (1)

Răzvan Puiu
Răzvan Puiu

Reputation: 731

I was able to achieve this using GetX. I store a boolean in the DB and have a Hasura subscription on it. As soon as it changes, the following controller gets called and the routing is being done.

class MaintenanceController extends GetxController {

 RxBool isMaintenance = false.obs;
    
      @override
      onInit() {
        super.onInit();
        ever(
            isMaintenance,
            (_) => {
                  if (isMaintenance.value) {Get.offNamed(Routes.maintenance)} else {Get.offNamed(Routes.splash)}
                });
      }
    }

The ever() method gets called whenever the value of the first parameter changes, and the second parameter gets executed. The method onInit() gets called upon the initialization of the controller. In my case, when I do Get.lazyPut(() => MaintenanceController()); By doing this, I am able to force the app to move between the maintenance screen and the splash screen - where I do all the initialization (without forcing a restart which might not be accepted by Apple).

Upvotes: 1

Related Questions