如何在 flutter 中从我的提供者 class 访问另一个 class 中的变量?
How do I access the variables from my provider class inside another class in flutter?
我正在尝试从我的提供商 class 调用 function googleLogin
,然后将其插入到我在 List text
中的方法中
class Data {
List text = [
{
"text": "Sign up with Google",
"icon": const Icon(
FontAwesomeIcons.google,
color: Colors.red,
size: 20,
),
"method": () {
print("the sign up button is working");
}
},
{
"text": "Sign up with Facebook",
"icon":
const Icon(FontAwesomeIcons.facebook, color: Colors.blue, size: 20),
"method": () {
print("signing up via facebook");
}
},
{
"text": "Sign up with email",
"icon":
const Icon(FontAwesomeIcons.envelope, color: Colors.white, size: 20),
"method": () {
print("signing up with email ");
}
},
{
"text": "Sign up as a Guest",
"icon": const Icon(FontAwesomeIcons.user, color: Colors.white, size: 20),
"method": () {
print("signing up as guest");
}
}
];}
这是我的提供商代码
class Auth extends ChangeNotifier
{
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? user;
Future googleLogin() async {
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
user = googleUser;
final googleAuth = await googleUser.authentication;
final credentials = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
await FirebaseAuth.instance.signInWithCredential(credentials);
}
}
然后您应该将 BuildContext 注入您的数据 class 并通过提供商获取 Auth,如下所示:
class Data {
Auth? auth;
Data(BuildContext ctx) {
auth = Provider.of<Auth>(context, listen: false);
}
/// then inside your List text…
List text = [
{
“method”: () async {
await auth!.googleLogin();
}
}
];
}
围绕这些线的东西。您也可以重构它,而不是将您的 List 文本包装到一个方法中,在该方法中您可以注入 Provider 甚至整个服务本身,以便您可以从内部调用它。我的两分钱。
我正在尝试从我的提供商 class 调用 function googleLogin
,然后将其插入到我在 List text
class Data {
List text = [
{
"text": "Sign up with Google",
"icon": const Icon(
FontAwesomeIcons.google,
color: Colors.red,
size: 20,
),
"method": () {
print("the sign up button is working");
}
},
{
"text": "Sign up with Facebook",
"icon":
const Icon(FontAwesomeIcons.facebook, color: Colors.blue, size: 20),
"method": () {
print("signing up via facebook");
}
},
{
"text": "Sign up with email",
"icon":
const Icon(FontAwesomeIcons.envelope, color: Colors.white, size: 20),
"method": () {
print("signing up with email ");
}
},
{
"text": "Sign up as a Guest",
"icon": const Icon(FontAwesomeIcons.user, color: Colors.white, size: 20),
"method": () {
print("signing up as guest");
}
}
];}
这是我的提供商代码
class Auth extends ChangeNotifier
{
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? user;
Future googleLogin() async {
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
user = googleUser;
final googleAuth = await googleUser.authentication;
final credentials = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
await FirebaseAuth.instance.signInWithCredential(credentials);
}
}
然后您应该将 BuildContext 注入您的数据 class 并通过提供商获取 Auth,如下所示:
class Data {
Auth? auth;
Data(BuildContext ctx) {
auth = Provider.of<Auth>(context, listen: false);
}
/// then inside your List text…
List text = [
{
“method”: () async {
await auth!.googleLogin();
}
}
];
}
围绕这些线的东西。您也可以重构它,而不是将您的 List 文本包装到一个方法中,在该方法中您可以注入 Provider 甚至整个服务本身,以便您可以从内部调用它。我的两分钱。