I'm trying to print a specific users info from firebase, but whenever I try to call the specific value, such as name, it always comes up as the error, Too many positional arguments: 0 expected, but 1 found.
getUserState() async{
String temp;
final FirebaseAuth auth = FirebaseAuth.instance;
final String id = FirebaseFirestore.instance.collection('users').id;
FirebaseFirestore.instance.collection('users').doc(id).get().then((value){
temp = value.data('name') as String;
});
CodePudding user response:
data
is a method that takes no parameter and returns a Map
Try this:
temp = value.data()!['name'] as String;
CodePudding user response:
To print an specific user information from Firebase calling by a value such as name, you can use the Firebase Database Query API. You can use the orderByChild() method to query the database and retrieve data based on a specific value. For example, if you want to print an user information from Firebase calling by name, you can use the following code:
// Get a reference to the database
var database = firebase.database();
// Query the database for user information based on name
var query = database.ref("users").orderByChild("name").equalTo("John Doe");
// Get the user information from the query
query.once("value", function(snapshot) {
var userInfo = snapshot.val();
// Print the user information
console.log(userInfo);
});
CodePudding user response:
You can use .get
to read map
temp = value.get('name') as String? ?? "" ;
CodePudding user response:
If you need a function that returns a String that contains the name you can refactor your code like the following:
Future<String?> getUserState(String userId) async {
final docRef = await FirebaseFirestore.instance.collection('users').doc(userId).get();
final user = docRef.data();
return user?['name'];
}
user: is a Map<String, dynamic> representation of your document in collection. You can use it to covert in your model with fromJson for example.
If any users found with provided id it returns null