我一直试图读取数据关闭Firestore,使用用户数据模型。如果我使用CollectionReference Type变量读取引用集合,并使用自定义构建的函数读取FutureBuilder中的文档列表,并将其直接Map到Map中,在我读取数据的同一个File中,它工作得很好。但是当我尝试使用我创建的User模型以User身份检索快照数据时,它显示了这个错误。
我尝试在用户模型中更改Map的定义,但不起作用,我做错了什么?
我的主页(我称之为读取数据)
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_project_1/screens/home/read_data.dart';
import 'package:firebase_project_1/screens/home/update_profile.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class Home extends StatefulWidget {
const Home({super.key});
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
final user = FirebaseAuth.instance.currentUser!;
// The list of all document IDs,
//which have access to each their own personal information
List<String> documentIDs = [];
// DocumentReference<Map<String, dynamic>>(Users/8bu245T440NIuQnJhm81)
// This is the sample output, to get IDs we just do .id
Future getDocIDs() async {
await FirebaseFirestore.instance
.collection('Users')
.orderBy('rank', descending: false)
.get()
.then((snapshot) => {
snapshot.docs.forEach((document) {
print(document.reference);
documentIDs.add(document.reference.id);
})
});
setState(() {});
}
@override
void initState() {
getDocIDs();
super.initState();
documentIDs = [];
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurple,
title: Center(
child: Text(
textAlign: TextAlign.center,
'Display dashboard',
style: TextStyle(
fontSize: 30,
color: Colors.tealAccent.shade400,
),
),
),
actions: [
GestureDetector(
onTap: () {
FirebaseAuth.instance.signOut();
},
child: const Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Icon(
Icons.logout_sharp,
),
),
)
],
),
backgroundColor: Colors.deepPurple.shade200,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// This has a FutureBuilder as it needs
// to wait for executiion of getDocIDs function to finish execution
Expanded(
child: Padding(
padding: const EdgeInsets.all(10.0),
child: ListView.builder(
itemCount: documentIDs.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(20),
side: const BorderSide(
width: 2,
color: Colors.indigo,
),
),
title: ReadData(docIDs: documentIDs[index]),
tileColor: Colors.indigo.shade300,
),
);
},
),
),
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => Get.to(() => UpdateProfile()) ,
backgroundColor: Colors.tealAccent,
child: const Icon(Icons.edit),
),
);
}
}
读取用户数据的我的文件
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_project_1/user_model/user_model.dart';
import 'package:flutter/material.dart';
class ReadData extends StatelessWidget {
const ReadData({required this.docIDs, super.key});
final String docIDs;
@override
Widget build(BuildContext context) {
// get the collection
CollectionReference users = FirebaseFirestore.instance.collection('Users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(docIDs).get(),
builder: (context, snapshot) {
//IF connection with the firebase is ensured, then we execute
if (snapshot.connectionState == ConnectionState.done) {
//We are trying to map the key and values pairs
//to a variable called "data" of Type Map
//Map<String, dynamic> data =
//snapshot.data!.data() as Map<String, dynamic>;
if (snapshot.hasData) {
User data = snapshot.data!.data() as User;
return Text(
//'${data['rationType']},' +
'${data.rationType}, ' +
' ' +
'Rank:' +
' ' +
//'${data['rank']}');
'${data.rank}');
}
}
return const Text('Loading......');
//Map<String, dynamic> data = User.fromJson(snapshot.data!.data())
//Returning the value for key called name
},
);
}
}
用户模型
import 'package:json_annotation/json_annotation.dart';
import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
@JsonSerializable(explicitToJson: true)
class User {
User({
required this.rank,
required this.appointment,
required this.rationType,
required this.status,
required this.mobileNumber,
required this.bloodgroup,
required this.dob,
required this.ord,
required this.attendence,
});
factory User.fromJson(Map<String, dynamic> json) {
return User(
rank: json['rank'],
appointment: json['appointment'],
rationType: json['rationType'],
status: json['status'],
mobileNumber: json['mobileNumber'],
bloodgroup: json['bloodgroup'],
dob: json['dob'],
ord: json['ord'],
attendence: json['attendence'],
);
}
final String rank;
final String appointment;
final String rationType;
final String status;
final String mobileNumber;
final String bloodgroup;
final String dob;
final String ord;
final String attendence;
Map<String, Object?> toJson() {
return {
'rank' : rank,
'appointment' : appointment,
'rationType' : rationType,
'status' : status,
'mobileNumber': mobileNumber,
'bloodgroup' : bloodgroup,
'dob' : dob,
'ord' : ord,
'attendence' : attendence,
};
}
}
1条答案
按热度按时间wwtsj6pe1#
尝试如下类型转换: