NoSuchMethodError: 在 null 上调用了方法“[]”。接收器:空。尝试调用:[]("duyuru")

时间:2021-07-10 13:12:44

标签: flutter dart listview getx flutter-getx

// 我的控制器

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);
          },
        );
      }
    });
  }
}

我的错误:未处理的异常:NoSuchMethodError:方法“[]”被调用为空值。接收器:空。尝试调用:。 即使我检查为空,列表也是空的,你能帮我吗?

enter image description here 我的 Firebase 数据库

我收到列表空检查错误,但我的数据库中有公告数据,我正在提取它们,但在屏幕上看不到数据

0 个答案:

没有答案