Home > other >  Pass data to new route
Pass data to new route

Time:10-03

I'm following a youtube video and trying to combine it with another class I took... I can't figure out what xxxx should be. Or if it's something else.

Home screen:

onPressed: () {
           Navigator.push(context, MaterialPageRoute(builder: (context) => Test(note: xxxx )));

          },
body: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection("notes")
              .where('userId', isEqualTo: user.uid)
              .snapshots(),
          builder: (context, AsyncSnapshot streamSnapshot) {
            if (streamSnapshot.hasData) {
              if (streamSnapshot.data.docs.length > 0) {
                return ListView.builder(
                    itemCount: streamSnapshot.data.docs.length ?? 0,
                    itemBuilder: (context, index) {
                    final NoteModel note =
                          NoteModel.fromJson(streamSnapshot.data.docs[index]);

Test screen:

class Test extends StatelessWidget {
  final NoteModel note;
  const Test({Key? key, required this.note}) : super(key: key);

Data model:

class NoteModel {
  String image;
  String id;
  String title;
  Timestamp date;
  String userId;

  NoteModel({
    required this.image,
    required this.id,
    required this.title,
    required this.date,
    required this.userId
  });

  factory NoteModel.fromJson(DocumentSnapshot snapshot){

    return NoteModel(
      image: snapshot['image'],
      id: snapshot.id,
      title: snapshot['title'],
      date: snapshot['date'], 
      userId: snapshot['userId']
      );   
  }
}

Here's the youtube video https://www.youtube.com/watch?v=iqDcGT3_zNA&ab_channel=dbestech if you want to check it out. He's passing DocumentSnapshot, I'm trying to pass NoteModel which may be the problem?

Edit

I changed my data model to this:

class NoteModel {

  String? image;
  ...

NoteModel({
    this.image,
    ...

Which allows me to do this without an underline error: Test(note: NoteModel()))); but I get a red screen that tells me Null check operator used on null value for Test widget...

Here is my test widget:

class Test extends StatelessWidget {
 
 NoteModel note;
 Test(this.note);

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      appBar: AppBar(title: Text("Testing"),),

      body: Container(
        width: MediaQuery.of(context).size.width,
        child: Column(children: [
          Text(
            note.title!)
        ]),
      ),

CodePudding user response:

xxx is the entire instance of NoteModel class so it expects somethig like this:

onPressed: () {
           Navigator.push(context, MaterialPageRoute(builder: (context) => Test(note: streamSnapshot.data )));

          },
...

This way you will be able to call all values from snapshot while in the Test page like this;

///Test
...
 @override
  Widget build(BuildContext context) {
    ...
    Text(widget.note.title.toString)
    ...
...

CodePudding user response:

As you have created a note from your snapshot data. you need to pass that NoteModel as you required for next screen

onPressed: () {
           Navigator.push(context, MaterialPageRoute(builder: (context) => Test(note: note )));
}
  • Related