我正在使用Bloc模式进行状态管理,并且当我使用WPUserInfoResponse方法访问用户数据以设置currentuser时,登录后尝试访问userpage上的currentuser时,我保持为空?
继续出错
The following NoSuchMethodError was thrown building UserPage(dirty, dependencies:
[InheritedProvider<Wordpress>], state: _UserPageState#df273):
The getter 'username' was called on null.
Receiver: null
Tried calling: username
The relevant error-causing widget was:
UserPage file:///F:/Flutter/mindmover/lib/providers/wordpress.dart:82:31
Wordpress changenotifier,其中有login和getuser方法,当用户在登录页面上登录时会调用它们。我已经检查了currentuser,它不是null。
enum LoginState { loggedin, signedout }
enum RoleState { admin, mentor, user }
enum ErrorState { invalidParams, serverIssue }
class Wordpress extends ChangeNotifier {
String userTokenId;
int errorMessage = 1;
WPUserInfo.Data currentuser;
LoginState loginState = LoginState.signedout;
get isLoggedIn => loginState == LoginState.loggedin;
get isSignOut => loginState == LoginState.signedout;
get errorMessageState => errorMessage;
WPUserInfo.Data get currentUserModel => currentuser;
dynamic lindawk =
WPJsonAPI.instance.initWith(baseUrl: "https://www.XXXX.dk");
Future login(String username, String password, context) async {
print(username.toString() + password.toString());
WPUserLoginResponse wpUserLoginResponse = await WPJsonAPI.instance.api(
(request) {
return request.wpLogin(
username: username,
password: password,
authType: WPAuthType.WpUsername);
},
);
if (wpUserLoginResponse != null) {
userTokenId = wpUserLoginResponse.data.userToken;
loginState = LoginState.loggedin;
getUser(userTokenId, context);
} else {
print("invalid login details");
errorMessage += 2;
}
}
getUser(String userTokenId, context) async {
WPUserInfo.WPUserInfoResponse wpUserInfoResponse =
await WPJsonAPI.instance.api(
(request) {
notifyListeners();
return request.wpGetUserInfo(userTokenId);
},
);
if (wpUserInfoResponse != null) {
print(wpUserInfoResponse.data.username);
currentuser = wpUserInfoResponse.data;
notifyListeners();
onValidUser(currentuser, context);
} else {
wpUserInfoResponse.message.toString();
errorMessage = 2;
print("something went wrong");
//errorMessage = "Something whent wrong";
}
}
void onValidUser(WPUserInfo.Data user, context) {
print(currentuser);
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => UserPage(
user: user,
),
),
);
}
}
在我有多个提供商的MyApp类中,播放器类工作正常,但wordpress类却不能。.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider<Player>.value(
value: Player(),
),
ChangeNotifierProvider<Wordpress>(
create: (_) { return Wordpress(); },
child: AppBottomNavigationBar(),
)
],
在用户页面上,我将其称为wordpress类,但是值username始终为null?
@override
Widget build(BuildContext context) {
Wordpress wordpress = Provider.of<Wordpress>(context);
return Scaffold(
appBar: AppBar(
actions: <Widget>[
FlatButton(
onPressed: () => {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => AppBottomNavigationBar(),
),
),
},
child: Icon(Icons.add_circle_outline),
),
],
),
body: Container(
child: Column(
children: <Widget>[
FlatButton(
onPressed: null,
child: Text(wordpress.currentUserModel.username),
),
],
),
),
);
}
答案 0 :(得分:0)
也许将类型提供给提供程序会给您一个实际的Wordpress类,还您在调用它之前是否检查过Wordpress类中的currentUserModel是否不为null?
@override
Widget build(BuildContext context) {
Wordpress wordpress = Provider.of<Wordpress>(context); //Give the type of value the provider should search in the tree
return Scaffold(
appBar: AppBar(
actions: <Widget>[
FlatButton(
onPressed: () => {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) => AppBottomNavigationBar(),
),
),
},
child: Icon(Icons.add_circle_outline),
),
],
),
body: Container(
child: Column(
children: <Widget>[
FlatButton(
onPressed: null,
child: Text(wordpress.currentUserModel.username), // without the type maybe provider can't give back the class and return null, null.currentUserModel.username gives the error
),
],
),
),
);
}