skip to Main Content

I have a Flutter app that runs a stored procedure in SQL Server using the sql_conn package. This SP will return the login state of the user. If they are suddenly restricted from being logged in, I want the Flutter app to know about this within 5 seconds.

Below is my code that calls the SP from Flutter. I now need it to run on a 5 second frequency and then handle what happens if the login state is ever 0 (logout and send user back to login screen). How would I do this?

void getLoginState() async {
    await sqlConnect();

    var res = await SqlConn.readData("declare @LoginState bit declare @Status nvarchar(4000) exec spHandheldLogin '${userController.text}', '${passwordController.text}', @LoginState output, @Status output");

    res = json.decode(res); //Gets the first object in the list. This is fine because this SP will only ever return 1 row
    res = res[0];
    var LoginState = res['LoginState'];
    var Status = res['Status'];

    await sqlDisconnect();
}

2

Answers


  1. Try this

    
    import 'dart:async';
    
    Timer? timer;
    
    @override
    void initState() {
      super.initState();
      timer = Timer.periodic(Duration(seconds: 10), (Timer t) => 
      getLoginState()); // Your function
    }
    
    @override
    void dispose() {
      timer?.cancel();
      super.dispose();
    }
    
    Login or Signup to reply.
  2. You can use stream and listen to the auth changes

    import 'dart:async';
    
    StreamSubscription? timerSubscription;
    
    @override
    void initState() {
      super.initState();
      timerSubscription = Stream.periodic(Duration(seconds: 10), (int count) {
        getLoginState(); // Your function
      }).listen((event) {});
    }
    
    @override
    void dispose() {
      timerSubscription?.cancel();
      super.dispose();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search