error
I tried to fetch user’s data from firebase using logged user uid . And also I used model
model code
Details detailsFromJson(String str) => Details.fromJson(json.decode(str));
class Details {
Details({
required this.id,
required this.age,
required this.drinkstalkSE,
required this.drinksUnderstandSE,
required this.familyChildren,
});
String id;
String age;
String drinkstalkSE;
String drinksUnderstandSE;
String familyChildren;
factory Details.fromJson(Map<String, dynamic> json) => Details(
id: json["id"] ?? "",
age: json["age"] ?? "",
drinkstalkSE: json["drinkstalkSE"] ?? "",
drinksUnderstandSE: json["drinksUnderstandSE"] ?? "",
familyChildren: json["familyChildren"] ?? "",
);
Map<String, dynamic> toJson() => {
"id": id,
"age": age,
"drinkstalkSE": drinkstalkSE,
"drinksUnderstandSE": drinksUnderstandSE,
"familyChildren": familyChildren,
};
}
backend code
bool loading = false;
@override
initState() {
super.initState();
loading = true;
getDetails();
}
Details? oneUserDetails;
Future<void> getDetails() async {
final sp = context.read<SignInProvider>();
final id = sp.uid;
final reference = FirebaseFirestore.instance.doc('users/$id');
final snapshot = reference.get();
final result = await snapshot.then(
(snap) => snap.data() == null ? null : Details.fromJson(snap.data()!));
print('result is ====> $result');
setState(() {
oneUserDetails = result;
loading = false;
});
}
screen code
Database image
In my code no any errors show I think problem have in my model but I couldn’t understand what is the error and how to solve it. How to solve this error?
2
Answers
According to my knowledge, there is a problem with the model. In the model, the list is drinksUnderstandSE and drinkstalkSE both are strings but in the database, it is the list so it should be a list.
The values you use are listed, you need to create a separate section in the class for them. If you don’t know exactly how, paste your json data on this site and it will handle it for you. You will see your mistake.
https://javiercbk.github.io/json_to_dart/