0

I have this class which accept docID by passing the value when calling the DatabaseService class.

However when i am trying to put this docID variable into my firebase query. It shows that my ${docID} is an instance member and cant be accessed in an initialier. Pleasae help me.

Data passed in like this

Navigator.push(
                  context,
                  MaterialPageRoute(
                  builder: (context) => announcementDetail(announcementlist.id),
                ),

announcementDetail class accepet the passed value

class announcementDetail extends StatelessWidget {

  final String docID;
  announcementDetail(this.docID);

  @override
  Widget build(BuildContext context) {
    return StreamProvider<List <Announcement>>.value(
      value: DatabaseService(uid: ' ', docID: docID).announcementDetailList,
      initialData: [],
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text('Home'),
          backgroundColor: Theme.of(context).primaryColor,
          elevation: 0.0,
          centerTitle: true,
        ),
        body: Container(
          child: Announcement_Detail_List(),
        ),
      ),
    );
  }
}

DatabaseService class accepts the value passed from announcementDetail class

class DatabaseService {

  final String? uid;
  final String? docID;
  final bool toggleDate = false;
  DatabaseService({ required this.uid, required this.docID});

  final Query<Map<String, dynamic>> getAnnouncementDetail = FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
  
  
  //announcementdetail list from snapshot
  List <Announcement> _announcementDetailListFromSnapshot(QuerySnapshot snapshot){
    return snapshot.docs.map((doc){
      return Announcement(
          id: doc.id,
          title: doc.get('title') ?? '',
          dateTime: doc.get('dateTime') ?? '',
          content: doc.get('content') ?? '',
          aType: doc.get('announcementType') ?? '',
          aLevel: doc.get('announcementLevel') ?? ''
      );
    }).toList();
  }

  // get announcementdetail stream
  Stream<List <Announcement>> get announcementDetailList {
    return getAnnouncementDetail.snapshots()
        .map(_announcementDetailListFromSnapshot);
  }
}

error part is here is DatabaseService Class

final Query<Map<String, dynamic>> getAnnouncementDetail = FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
Steve Kush
  • 143
  • 1
  • 13

1 Answers1

1

The problem is that you're trying to use the docID field of your DatabaseService class in the initializer of a field, which runs before its constructor is called. Since the constructor is where this.docID gets its value, you cannot use this value before the constructor has run.

The solution is to initialize the getAnnouncementDetail field inside the constructor:

class DatabaseService {
  final String? uid;
  final String? docID;
  final bool toggleDate = false;
  late Query<Map<String, dynamic>> getAnnouncementDetail;

  DatabaseService({ required this.uid, required this.docID}) {
    getAnnouncementDetail = FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
  }
  ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807