gpt4 book ai didi

flutter - 如何在 flutter 中实现可观察的消费者(听众)

转载 作者:行者123 更新时间:2023-12-03 03:39:39 27 4
gpt4 key购买 nike

我还在学习 flutter 。以下是本教程中的代码。当我调用Authservice配置文件时,它返回Observable<Map<String, dynamic>>。如何获取其中的数据以进行显示。

这是我到目前为止所做的事情,但是它返回了整个块,但我想分开获取每一对。例如电子邮件,姓名等。

AuthService.profile.listen((value){
print(value);
});
//This is my solution so far
---------------------------------------------------------------------

//This is my code

import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:rxdart/rxdart.dart';

class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
final Firestore _db = Firestore.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();

Future<FirebaseUser> get getUser => _auth.currentUser();
//Stream<FirebaseUser> get user => _auth.onAuthStateChanged;

Observable<FirebaseUser> user; //firebase user
Observable<Map<String, dynamic>> profile; //custom user data in firestore
PublishSubject loading = PublishSubject(); // Push new value manulay

AuthService() {
user = Observable(_auth.onAuthStateChanged);
profile = user.switchMap((FirebaseUser u) {
if (u != null) {
return _db
.collection('users')
.document(u.uid)
.snapshots()
.map((snap) => snap.data);
} else {
return Observable.just({});
}
});
}```

最佳答案

这是一个有关如何在Dart中访问 类型的Map的简单示例:

foo() {
Map<String, dynamic> user = {
"uid":7,
"username": "nasser",
};

print('${user.toString()}'); // {uid: 7, username: nasser}
print('${user['uid']}'); // 7

// You can loop inside Map and do whatever
// you want with the key and the value; print them,
// Or create a UserModel object from them..:
user.forEach((key, value) => print('$key: $value'));
// forEach's output:
// uid: 7
// username: nasser
}

更新问题的具体情况:

根据此代码:

AuthService.profile.listen((value){
print(value);
});

您正在访问Class本身,它将无法正常工作(除非功能'profile'是静态的,这不是此处),因此 首先需要在使用之前从“AuthService”类创建一个实例。而且,由于您需要在所有应用程序中使用相同的对象,因此,需要在内具有“AuthService”类的文件的末尾创建一个共享对象 ,请参阅并阅读评论:

class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
final Firestore _db = Firestore.instance;
final GoogleSignIn _googleSignIn = GoogleSignIn();
//..
//..
} // THIS IS THE CLOSING OF "class AuthService {"

// Add this at the end of the file that you are
// saving "AuthService" class in, SO YOU WILL BE CREATING
// A SHARED OBJECT(KINDA BLOC) that you can access from
// other classes without creating a new instance, just
// by importing "AuthService class" file.
// This thing has a nickname called 'Singleton' but needs some
// little work to be a perfect Singleton, but it's enough here:
final authService = AuthService();

现在可以使用“authService”对象。因此,将在小部件中使用用户配置文件数据(例如homeScreen或),需要导入包含“AuthService类”和 final authService = AuthService();的文件。

然后随意访问已创建的
uthService对象 (没有 uthService类,请参阅小写字母),并通过import 'auth_service.dart';在“HomeScreen”中将其使用,请参见以下代码和注释:

// Import AuthService class, I assume that this is the file name and location
import 'auth_service.dart';

class HomeScreen extends StatefulWidget {
@override
HomeScreenState createState() => HomeScreenState();
}

class HomeScreenState extends State<HomeScreen> {
// Define a var with same type as the Observable to store its data:
Map<String, dynamic> _userProfile;

@override
initState() {
super.initState();

// Listen to Observable which called "profile" and once this
// Observable get updated, it will update "_userProfile" too,
// setState here is important, otherwise you will need a streamBuilder
// to see the changes on screen:
authService.profile.listen((v) => setState(() => _userProfile = v));
}

@override
Widget build(BuildContext context) {

// WAIT TILL THE FETCHING FROM FIREBASE FINISH
// THEN YOU WILL BE ABLE TO SEE THE RESULT
print('${_userProfile.toString()}');

_userProfile.forEach((key, value) => print('$key: $value'));

return Container(
child: Text(
_userProfile.toString(),
),
);
}
}

关于flutter - 如何在 flutter 中实现可观察的消费者(听众),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58088402/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com