我正在使用提供程序包进行状态管理, 我不知道此问题是否是由于提供程序造成的,还是我在这里丢失了一些东西,我感谢能发现我的错误的人,
这是我的ChangeNotifier类
class Db extends ChangeNotifier {
bool _fetchingData = false;
User _user;
User get getCurrentUser{
return _user;
}
set setUser(User user){
print('db_class.dart 15: Setting the user');
_user = user;
}
void setFetchingData(bool state){
_fetchingData = state;
notifyListeners();
}
bool get isFetching{
return _fetchingData;
}
}
请关注这个吸气剂
User get getCurrentUser{
return _user;
}
使用此类的createUser方法创建该用户后,我应该获得该用户的实例
class Auth extends Db {
Future<IdTokenResult> signIn(String email, String password) {...}
Future<IdTokenResult> signUp(String email, String password) {...}
Future<void> signOut() {...}
static bool isTokenExpired(DateTime dateTime) {{...}
void createUser(IdTokenResult idToken) {
print('auth.dart 53: creatUser method is called');
User user;
switch (idToken.claims['claim']) {
case 'Admin':
setUser = Admin(
uid: idToken.claims['user_id'],
email: idToken.claims['email'],
token: idToken.claims['token'],
claim: ClaimsType.Admin,
phoneNumber: idToken.claims['phone_number']);
break;
case 'ShopOwner':
setUser = ShopOwner(
uid: idToken.claims['user_id'],
email: idToken.claims['email'],
token: idToken.claims['token'],
claim: ClaimsType.ShopOwner,
shopName: idToken.claims['name'],
phoneNumber: idToken.claims['phone_number'],
//ToDo: get first name and last
firstName: 'amjed',
lastName: 'al anqoodi',
ownerPhotoUrl: idToken.claims['photoUrl']);
break;
default:
setUser = Customer(
uid: idToken.claims['user_id'],
email: idToken.claims['email'],
token: idToken.claims['token'],
phoneNumber: idToken.claims['phone_number'],
userDisplayName: 'test',
userRealName: 'amjed san',
userFamilyName: 'al anqoodi',
country: 'oman',
city: 'nizwa',
village: 'marfa daris');
break;
}
print('auth.dart 94: ${getCurrentUser.email}');
// print('auth.dart 75: ${user.email}');
}
}
问题是,当我在添加产品页面中调用getter getCurrentUser时,我得到一个空值,为什么?
class AddProductPage extends StatefulWidget {
@override
_AddProductPageState createState() => _AddProductPageState();
}
class _AddProductPageState extends State<AddProductPage> {
List<Asset> images = List<Asset>();
String _error;
TextEditingController productNameController = TextEditingController();
TextEditingController priceController = TextEditingController();
@override
void initState() {
super.initState();
}
Widget buildGridView() {...}
Future<void> loadAssets() async {...}
@override
Widget build(BuildContext context){
var db = Provider.of<Db>(context);
return Padding(
padding: const EdgeInsets.all(15.0),
child: Center(
child: Column(
children: <Widget>[
Text('Shop Owner Page'),
TextField(
controller: productNameController,
decoration: InputDecoration(
hintText: 'Product Name'
),
),
TextField(
controller: priceController,
decoration: InputDecoration(
hintText: 'Product Price'
),
),
Center(child: Text('Error: $_error')),
RaisedButton(
child: Text("Pick images"),
onPressed: loadAssets,
),
Expanded(
child: buildGridView(),
),
RaisedButton(
child: Text('Add The Product'),
onPressed: () async{
if(db.getCurrentUser == null){
print('add_product 103: user is null');
}else{
print('add_product 105: user is not null');
}
print('add_product 107: ${db.getCurrentUser.email}');
print('add_product 108: ${(db.getCurrentUser as ShopOwner).shopName}');
// ShopsManagement shopsManagement = ShopsManagement();
// await shopsManagement.addProduct(claim: (auth.getCurrentUser as ShopOwner).claim, shopName: (auth.getCurrentUser as ShopOwner).shopName , productName: productNameController.text, price: double.parse(priceController.text), assets: images);
},
),
],
),
),
);
}
}
问题就在这里,当我调用onPress方法时,我在“ db.getCurrentUser”上得到的是空值,
RaisedButton(
child: Text('Add The Product'),
onPressed: () async{
if(db.getCurrentUser == null){
print('add_product 103: user is null');
}else{
print('add_product 105: user is not null');
}
print('add_product 107: ${db.getCurrentUser.email}');
print('add_product 108: ${(db.getCurrentUser as ShopOwner).shopName}');
// ShopsManagement shopsManagement = ShopsManagement();
// await shopsManagement.addProduct(claim: (auth.getCurrentUser as ShopOwner).claim, shopName: (auth.getCurrentUser as ShopOwner).shopName , productName: productNameController.text, price: double.parse(priceController.text), assets: images);
},
),
答案 0 :(得分:0)
我认为您必须在_user = user;
方法的这一行setUser
之后调用notifyListeners。