You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter登录验证后页面不加载及两类异常问题求助

Hey there! Let's break down your issues one by one and fix them step by step:

1. Fixing the ArgumentError in LoginScreenPresenter.doLogin

The error message tells us your error handler in catchError doesn’t accept the correct arguments. Right now, you’re using (Exception error) but Dart’s catchError expects a handler that takes either an Object or an Object plus a StackTrace.

Update your login_screen_presenter.dart code:

doLogin(String username, String password) {
  api.login(username, password).then((User user) {
    _view.onLoginSuccess(user);
  }).catchError((Object error) => _view.onLoginError(error.toString()));
  // Optional: Capture stack trace for more detailed debugging
  // }).catchError((Object error, StackTrace stackTrace) => _view.onLoginError("$error\n$stackTrace"));
}

2. Fixing the "Boolean expression must not be null" Exception

This comes from rest_ds.dart where you check if(res["error"]). On successful login, your API response doesn’t include an error key, so res["error"] returns null — and you can’t use a null value in a boolean check.

Modify the login method in rest_ds.dart to handle this safely:

Future<User> login(String username, String password) {
  return _netUtil.post(LOGIN_URL, body: {
    "username": username,
    "password": password
  }).then((dynamic res) {
    print(res.toString());
    // Check if "error" exists and is truthy
    if(res.containsKey("error") && res["error"]){ 
      throw new Exception(res["error_msg"]);
    }
    // Alternative: Use null coalescing to avoid null values
    // if(res["error"] ?? false){ throw new Exception(res["error_msg"]); }
    return new User.map(res["user"]);
  });
}

3. Cleaning Up Redundant Code in onLoginSuccess

Your onLoginSuccess method has duplicate, unused code (the stray HomeScreen() calls) and you’re manually triggering onAuthStateChanged after notifying the provider. Let’s clean that up:

@override
void onLoginSuccess(User user) async {
  setState(() => _isLoading = false);
  _showSnackBar(user.toString());
  
  var db = new DatabaseHelper();
  await db.saveUser(user);
  
  var authStateProvider = new AuthStateProvider();
  authStateProvider.notify(AuthState.LOGGED_IN);
  // Remove the manual call — your listener will trigger navigation automatically
  // onAuthStateChanged(AuthState.LOGGED_IN); 
}

4. Verify Route Registration

Double-check that you’ve properly registered the /home route in your app’s root MaterialApp:

MaterialApp(
  routes: {
    "/home": (context) => HomeScreen(),
    // Add other routes here
  },
)

5. Ensure Safe Navigation Context

Instead of relying on the _ctx variable, use the widget’s built-in context directly in your auth state listener for more reliability:

@override
onAuthStateChanged(AuthState state) {
  if(state == AuthState.LOGGED_IN) {
    Navigator.of(context).pushReplacementNamed("/home");
  }
}

After applying these fixes, your loading animation should stop once login succeeds, and the app will navigate to the HomeScreen without those exceptions.

内容的提问来源于stack exchange,提问作者Sarang Rastogi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:17:50