Issue
Goal: I want to pass the uid
which i get from my homescreen.dart
to my enroll.dart
and access data inside the firestore collection using the passed uid
Here's how i pass data from homescreen.dart
to enroll.dart
Future navigate(String id, String name) async {
Navigator.push(
context,
MaterialPageRoute(
builder: ((context) => enroll(
uid: id,
name: name,
))));
}
Here's how i access the data in my enroll.dart
class enroll extends StatefulWidget {
final String uid;
final String name;
enroll({Key? key, required this.uid, required this.name}) : super(key: key);
@override
State<enroll> createState() => _enrollState();
}
class _enrollState extends State<enroll> {
final CollectionReference programs =
FirebaseFirestore.instance.collection(widget.uid); //This how i access the data
}
Prolem: When i try to do so,it shows The instance member 'widget' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
I tried giving the text widget the same by Text(widget.uid)
and it work fine.
Iam new to flutter,Any help is Appreciated
Solution
You cannot use a value from widget ('uuid' here) before the method 'initState' of '_enrollState'. You need to wait the widget to be initialized.
You could do
class _enrollState extends State<enroll> {
late final CollectionReference programs;
@override
void initState(){
super.initState();
programs = FirebaseFirestore.instance.collection(widget.uid);
}
}
Answered By - VincentDR
Answer Checked By - Katrina (JavaFixing Volunteer)