我如何添加另一个提供者,并从Firebase的值填充其变量?



我遵循了一个关于如何在应用程序周围使用包装器来监视用户身份验证状态的教程。这里我用的是StreamProvider。我现在想添加一个新的provider来保存一些全局变量(2个字符串字段)。这些变量应该保存用户经过身份验证的UID(来自Firebase Auth)和用户名(来自Firestore)。我试图按照本教程添加它,但它不工作。

我如何添加这个provider和填充它的值从Firebase Auth和Firestore?

当前代码:

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseHelper.init();
runApp(new MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return StreamProvider<ModelUser?>.value(
value: AuthService().user,
initialData: null,
child: GetMaterialApp(
title: 'MyApp',
home: Wrapper(),
),
);
}
}

我想添加的教程部分:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ThemeProvider>(
create: (context) => ThemeProvider(),
child: Consumer<ThemeProvider>(
builder: (context, themeProvider, child) => MaterialApp(
title: 'Flutter Provider Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
appBarTheme: AppBarTheme(brightness: Brightness.dark),
),
home: MainScaffold(),
),
),
);
}
}

我想使用的提供者:

class GlobalsProvider extends ChangeNotifier {
String currentUID = '';
String currentUsername = '';
}

Wrapper.dart:

class Wrapper extends StatelessWidget {
const Wrapper({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final user = Provider.of<ModelUser?>(context);
//return either Home or Authenticate widget
if (user == null) {
return Authenticate();
} else {
return Home();
}
}
}

我已经尝试过了:

更新:错误是由其他原因引起的。应用程序运行了,现在我所需要的就是知道如何用Firebase的值填充这些提供商变量。

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<GlobalsProvider>(
create: (context) => GlobalsProvider(),
child: Consumer<GlobalsProvider>(
builder: (context, globalsProvider, child) => StreamProvider<ModelUser?>.value(
value: AuthService().user,
initialData: null,
child: GetMaterialApp(
title: 'MyApp',
home: Wrapper(),
),
),
),
);
}
}

您可以定义您的GlobalsProvider并通过Provider提供服务,如下所示:

Update我已经更新了代码

globals_provider.dart:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
class GlobalsProvider extends ChangeNotifier {
String currentUID = '';
String currentUsername = '';
GlobalsProvider() {
userGlobals();
}
Future<void> userGlobals() async {
final user = FirebaseAuth.instance.currentUser;
if (user != null) {
currentUID = user.uid;
final userData = await FirebaseFirestore.instance.collection('users').doc(user.uid).get();
currentUsername = userData.data()?['username'] ?? '';
}
notifyListeners();
}
}

这里userName不存在于auth.currentUser,所以我假设你是在用户集合中存储用户详细信息,并将其与auth.currentUser存储为文档id,但如果你的意思是将displayName分配给currentUsername,那么你可以这样做,就像currentUID一样:currentUsername = user.displayName;

Provider调用GlobalProvider类的userGlobals()方法为此,您需要将此依赖项添加到您的pubspec.yaml

那么你可以像下面这样为包装器提供GlobalProvider类:main.dart:

import 'package:provider/provider.dart';
import './globals_provider.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<GlobalsProvider>.value(
value: GlobalsProvider(),
child: MaterialApp(
title: 'My App',
home: Wrapper(),
),
);
}
}
class Wrapper extends StatelessWidget {
const Wrapper({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final globalsProvider = Provider.of<GlobalsProvider>(context);
globalsProvider.userGlobals();
final user = Provider.of<ModelUser?>(context);
//return either Home or Authenticate widget
if (user == null) {
return Authenticate();
} else {
return Home();
}
}
}

Update在你的数据库中。您可以创建GlobalsProvider类的新实例,并访问currentUID和currentUsername变量:

import './globals_provider.dart';
class Database {
GlobalsProvider globalsProvider = GlobalsProvider();
Future<void> someDatabaseFunction() async {
// Call the userGlobals() method to initialize your required variables
await globalsProvider.userGlobals();
// Access the variables
print(globalsProvider.currentUID);
print(globalsProvider.currentUsername);
}
}

Reference:Provider

相关内容

  • 没有找到相关文章

最新更新