在我的 main.dart 页面中,我使用 MultiProvider,如下所示。我在 main.dart 中初始化了前两个提供程序,并在用户页面中初始化了 UserInfoProvider。
在 initState 之后的用户页面中,我从远程数据库获取数据并进行初始化,如下所示。但是当我调用每个提供者的值时,前 2 个提供者返回一个值,但 UserInfoProvider 返回一个空值。
如何获取 UserInfoProvider _currentUserPhone 值?
UPDAE:(1)不工作部分: 当我尝试从提供者获取值时,下面的初始化不起作用,因为我正在创建另一个实例对象。正确的方法如下:
UserInfoProvider 初始化:
UserInfoProvider userInfoProvider = UserInfoProvider();
userInfoProvider.initUserInfo(
"$_currentCustomerNo",
"$_currentCustomerToken",
"$_currentCustomerName",
"$_currentUserMobile",
"$_currentUserEmail",
"$_currentUserAddress",
1);
更新:(2)工作部分:
Provider.of<UserInfoProvider>(context, listen: false).userMobile = _currentUserMobile;
show_user_data.dart:
/// (1) INIT STATE
@override
void initState() {
super.initState();
setState(() {
// TODO: GET DEVICE TYPE from PROVIDER
deviceTypeMobile = Provider.of<DeviceInfoProvider>(context, listen: false).deviceMobile;
// TODO: GET APP VERSION from PROVIDER
final String _tempBuildNo = Provider.of<PackageInfoProvider>(context, listen: false).buildNumber;
_appBuildNumber = int.parse(_tempBuildNo.toString().trim());
// TODO: GET USER INFO from PROVIDER
_currentUserPhone = Provider.of<UserInfoProvider>(context, listen: false).userMobile;
});
print("\n\n");
print("===========================================================================");
print("===========================================================================");
print("deviceTypeMobile: $deviceTypeMobile");
print("_appBuildNumber: $_appBuildNumber");
print("_currentUserPhone: $_currentUserPhone");
print("===========================================================================");
print("===========================================================================");
}
结果:
===========================================================================
===========================================================================
deviceTypeMobile: true
_appBuildNumber: 47
_currentUserPhone: null
===========================================================================
===========================================================================
主要.dart
void main() async {
PackageInfoProvider packageInfoProvider = PackageInfoProvider();
packageInfoProvider.initPackageInfo();
DeviceInfoProvider deviceInfoProvider = DeviceInfoProvider();
deviceInfoProvider.initDeviceInfo();
//runApp(NTMSApp());
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => packageInfoProvider),
ChangeNotifierProvider(create: (_) => deviceInfoProvider),
ChangeNotifierProvider(create: (_) => UserInfoProvider())
],
child: NTMSApp(),
),
);
}
package_info_provider.dart
class PackageInfoProvider with ChangeNotifier {
String appName = "";
String packageName = "";
String version = "";
String buildNumber = "";
Future<void> initPackageInfo() async {
final PackageInfo info = await PackageInfo.fromPlatform();
appName = info.appName;
packageName = info.packageName;
version = info.version;
buildNumber = info.buildNumber;
notifyListeners();
}
}
user_info_provider.dart
class UserInfoProvider extends ChangeNotifier {
String userNo = "";
String userToken = "";
String userName = "";
String userMobile = "";
String userEmail = "";
String userAddress = "";
int userAuthority = 0;
void initUserInfo(
String currentUserNo,
String currentUserToken,
String currentUserName,
String currentUserMobile,
String currentUserEmail,
String currentUserAddress,
int userAuthority) {
userNo = currentUserNo;
userToken = currentUserToken;
userName = currentUserName;
userMobile = currentUserMobile;
userEmail = currentUserEmail;
userAddress = currentUserAddress;
userYetki = currentUserYetki;
notifyListeners();
}
}
更新:
在 UserInfoProvider 中,我将所有变量声明为 String userMobile;按照建议。我从数据库中获取了值,并成功打印了 (1)。我还在 (2) notifyListeners(); 之前打印了值 我还在初始化用户提供程序后打印 (3) 值。但是在我的另一页 (4) 中,我无法打印该值。
(1) +++++++++++++++++++++++++++++++++++++++++
flutter: IN data from socket API - before Provider Initialise
flutter: _currentUserMobile: 53******90
flutter: +++++++++++++++++++++++++++++++++++++++++
flutter:
(2) +++++++++++++++++++++++++++++++++++++++++
flutter: IN void initUserInfo() in class UserInfoProvider
flutter: currentUserMobile: 53******90
flutter: userMobile: 53******90
flutter: +++++++++++++++++++++++++++++++++++++++++
flutter:
(3) +++++++++++++++++++++++++++++++++++++++++
flutter: IN data from socket API - after Provider Initialise
flutter: _currentUserMobile: 53******90
flutter: +++++++++++++++++++++++++++++++++++++++++
flutter:
flutter:
(4) +++++++++++++++++++++++++++++++++++++++++
flutter: deviceTypeMobile: false
flutter: _appBuildNumber: 67
flutter: _currentUserPhone: null
+++++++++++++++++++++++++++++++++++++++++
flutter:
flutter: