簡體   English   中英

Firebase stream 返回空列表

[英]Firebase stream returns empty list

在我的firestore db中,我有一個users集合,其中有一個arrays的集合。 我能夠成功地將一個新數組添加到 arrays 的集合中,但問題出在我的ArrayController中,Firestore Stream 返回一個空列表,即使集合中有文檔。 我想要的是在列表視圖中顯示Arrays

數組控制器

class ArrayController extends GetxController {
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;

  late CollectionReference collectionReference;

  RxList<ArrayModel> arrays = RxList<ArrayModel>([]);

  @override
  void onInit() {
    super.onInit();
    String uid = Get.find<AuthController>().user!.uid;
    collectionReference =
        _firestore.collection("users").doc(uid).collection("arrays");
    arrays.bindStream(getAllArrays()); // empty list
  }

  Stream<List<ArrayModel>> getAllArrays() {
    return collectionReference.snapshots().map(
        (query) => query.docs.map((item) => ArrayModel.fromMap(item)).toList());
  }
}

數組模型

class ArrayModel {
  String? title;
  String? id;
  List<Todo>? todos;
  Timestamp? dateCreated;

  ArrayModel({this.title, this.id, this.todos, this.dateCreated});

  ArrayModel.fromMap(DocumentSnapshot doc) {
    title = doc["title"];
    id = doc.id;
    todos = doc["todos"];
    dateCreated = doc["dateCreated"];
  }
}

列表顯示

GetX<ArrayController>(
                            init: Get.put<ArrayController>(ArrayController()),
                            builder: (ArrayController arrayController) {
                              return ListView.separated(
                                  itemBuilder: (context, index) =>
                                      GestureDetector(
                                        onLongPress: () {
                                          Navigator.of(context).push(
                                              Routes.routeToArrayScreenIndex(
                                                  index));
                                        },
                                        onTap: () {
                                          Navigator.of(context).push(
                                              Routes.routeToHomeScreen(index));
                                        },
                                        child: Dismissible(
                                          key: UniqueKey(),
                                          onDismissed: (_) {
                                            HapticFeedback.heavyImpact();
                                            Database().deleteArray(
                                                uid,
                                                arrayController
                                                        .arrays[index].id ??
                                                    '');
                                          },
                                          child: Container(
                                            child: Padding(
                                              padding: const EdgeInsets.only(
                                                  right: 25.0),
                                              child: Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceBetween,
                                                children: [
                                                  Padding(
                                                    padding:
                                                        const EdgeInsets.only(
                                                            left: 20.0),
                                                    child: Text(
                                                      arrayController
                                                              .arrays[index]
                                                              .title ??
                                                          '',
                                                      style:
                                                          GoogleFonts.notoSans(
                                                              color:
                                                                  Colors.white,
                                                              fontSize: 25.0),
                                                    ),
                                                  ),
                                                  Padding(
                                                    padding:
                                                        const EdgeInsets.only(
                                                            left: 20.0),
                                                    child: Text(
                                                      '0',
                                                      style:
                                                          GoogleFonts.notoSans(
                                                              color:
                                                                  primaryColor,
                                                              fontSize: 27.0),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                  separatorBuilder: (_, __) => const SizedBox(
                                        height: 15.0,
                                      ),
                                  itemCount: arrayController.arrays.length);
                            })

刪除init: Get.put<ArrayController>(ArrayController())

此外,如果您嘗試訪問當前登錄的用戶,請執行以下操作:

Stream<List<ArrayModel>> getAllArrays() {
String uid =  FirebaseAuth.instance.currentUser.uid; // i think you can declare this outside the scope
    return  _firestore.collection("users").doc(uid).collection("arrays").snapshots().map(
        (query) => query.docs.map((item) => ArrayModel.fromMap(item)).toList());
  }

您能否檢查您的字段是否存在錯誤 state 元素不存在:

ArrayModel.fromMap(DocumentSnapshot doc) {
    title = doc["title"]; //check for bad state on your fields
    id = doc.id;
    todos = doc["todos"];
    dateCreated = doc["dateCreated"];
  }

也試試這個:

class GlobalBindings implements Bindings {
  @override
  void dependencies() {
    Get.put<ArrayController>(ArrayController());
    //Include your other controllers here
 }
}
//Set it in your main
void main() async {
  WidgetsFlutterBinding.ensureInitialized(); 
  await Firebase.initializeApp();  
  GlobalBindings().dependencies();
  runApp(MyApp());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM