//我的duyuru控制器
class DuyuruController extends GetxController {
FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
TextEditingController duyuru = TextEditingController();
Rx<List<DuyuruModel>> duyurumodel = Rx<List<DuyuruModel>>([]);
List<DuyuruModel> get newsModelList => duyurumodel.value;
@override
void onInit() {
duyurumodel.bindStream(todoStream(uid));
super.onInit();
}
var uid;
String collection = "duyuru";
firestoreduyuruekle(String id) {
uid = new Uuid().v4().toString();
_firebaseFirestore.collection(collection).doc(uid).set({
"kullaniciid": id,
"duyuruid": uid,
"duyuru": duyuru.text,
});
duyuru.clear();
}
Stream<List<DuyuruModel>> todoStream(String uid) {
return _firebaseFirestore
.collection(collection)
.doc(uid)
.snapshots()
.map((query) {
var data = query.data();
List veriler = data['duyuru'];
List<DuyuruModel> retVal = [];
veriler.forEach((element) {
retVal.add(DuyuruModel.fromMap(element));
});
return retVal;
});
}
}
// 我的模型
class DuyuruModel {
static const ID = "duyuruid";
static const NAME = "duyuru";
String id;
String duyuru;
DuyuruModel({
this.id,
this.duyuru,
});
DuyuruModel.fromMap(DocumentSnapshot documentSnapshot) {
id = (documentSnapshot.data() as Map)[ID];
duyuru = (documentSnapshot.data() as Map)[NAME];
}
}
// 我的用户界面
class DuyuruWidget extends StatefulWidget {
@override
_DuyuruWidgetState createState() => _DuyuruWidgetState();
}
class _DuyuruWidgetState extends State<DuyuruWidget> {
var deger2 = Get.put<AuthController>(AuthController());
var deger1 = Get.put<DuyuruController>(DuyuruController());
@override
Widget build(BuildContext context) {
return Obx(() {
if (deger1.newsModelList == null) {
return Text('Loading');
} else if (deger1.newsModelList.isEmpty) {
return Text('Empty List');
} else {
return ListView.builder(
itemCount: deger1.newsModelList.length,
itemBuilder: (context, index) {
return Text(deger1.newsModelList[index].duyuru);
},
);
}
});
}
}
// addDuyuru
class DuyuruEklemeSayfasi extends StatelessWidget {
var deger = Get.put(DuyuruController());
var kontrol = Get.put(AuthController());
final GlobalKey<FormState> control = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Duyuru Ekle"),
),
body: Container(
child: Form(
key: control,
child: Column(
children: [
TextFormField(
validator: (value) {
if (value == null && value.isEmpty) {
Get.snackbar("Hata", "Lütfen Duyuru giriniz");
}
return;
},
controller: deger.duyuru,
decoration: InputDecoration(
hintText: "Duyuru Ekle",
),
),
ElevatedButton(
onPressed: () {
deger.firestoreduyuruekle(kontrol.usermodel.value.id);
Get.to(HomeScreen());
},
child: Text("Duyuruyu Ekle"),
),
],
),
),
),
);
}
}
// Duyuruwidget
class DuyuruWidget extends StatefulWidget {
@override
_DuyuruWidgetState createState() => _DuyuruWidgetState();
}
class _DuyuruWidgetState extends State<DuyuruWidget> {
var deger2 = Get.put<AuthController>(AuthController());
var deger1 = Get.put<DuyuruController>(DuyuruController());
@override
Widget build(BuildContext context) {
return Obx(() {
if (deger1.newsModelList == null) {
return Text('Loading');
} else if (deger1.newsModelList.isEmpty) {
return Text('Empty List');
} else {
return ListView.builder(
itemCount: deger1.newsModelList.length,
itemBuilder: (context, index) {
return Text(
deger1.newsModelList[index].duyuru,
style: TextStyle(color: Colors.black),
);
},
);
}
});
}
}
// 用户控制器
class AuthController extends GetxController {
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseFirestore _firebaseFirestore = FirebaseFirestore.instance;
TextEditingController emailtext = TextEditingController();
TextEditingController password = TextEditingController();
Rx<User> user;
Rx<UserModel> usermodel = UserModel().obs;
String usercollection = "users";
String duyurucollection = "duyuru";
@override
void onReady() {
user = Rx<User>(auth.currentUser);
user.bindStream(auth.userChanges());
ever(user, setscreen);
super.onReady();
}
setscreen(User user) {
if (user == null) {
Get.to(AuthenScreen());
} else {
usermodel.bindStream(listentouser());
Get.to(HomeScreen());
}
}
void signIn() async {
try {
await auth
.signInWithEmailAndPassword(
email: emailtext.text.trim(), password: password.text.trim())
.then((result) {
_clearControllers();
});
} catch (e) {
debugPrint(e.toString());
Get.snackbar("Sign In Failed", "Try again");
}
}
void signUp() async {
try {
await auth
.createUserWithEmailAndPassword(
email: emailtext.text.trim(), password: password.text.trim())
.then((result) {
String _userId = result.user.uid;
_addUserToFirestore(_userId);
_clearControllers();
});
} catch (e) {
debugPrint(e.toString());
Get.snackbar("Sign In Failed", "Try again");
}
}
void signOut() async {
await auth.signOut();
}
void _addUserToFirestore(String userId) {
_firebaseFirestore.collection(usercollection).doc(userId).set({
"password": password.text.trim(),
"id": userId,
"email": emailtext.text.trim(),
});
}
Stream<UserModel> listentouser() {
return _firebaseFirestore
.collection(usercollection)
.doc(user.value.uid)
.snapshots()
.map((event) => UserModel.fromSnapshot(event));
}
void _clearControllers() {
emailtext.clear();
password.clear();
}
}
我的错误:未处理的异常:NoSuchMethodError:方法“[]”被调用为空值。接收器:空。尝试调用:。即使我检查为空,列表也是空的,你能帮我吗?我无法解决。
我的 firestore duyuru 数据库
我的 Firestore 用户数据库
我收到列表空检查错误,但我的数据库中有公告数据,我正在提取它们,但在屏幕上看不到数据