Home > Blockchain >  cannot pass value from one class to another
cannot pass value from one class to another

Time:08-02

I have two classes.First class consist of list from the firestore. I want to pass value from first class to second class. I did create a constructor in the second class to get the value from the first class but i am getting null value in second class. I want to pass value of snap[index]['url'] to another class which has constructor url.

*first class*
Widget _html() {

  return Column(
    children: [
      const Text("\nWorship"),
      StreamBuilder<QuerySnapshot>(
        stream: FirebaseFirestore.instance.collection("Worship").snapshots(),
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if(snapshot.hasData) {
            final snap = snapshot.data!.docs;
            return ListView.builder(
              shrinkWrap: true,
              primary: false,
              itemCount: snap.length,
              itemBuilder: (context, index) {
                         return Stack(
                    children: [
                      GestureDetector(
                        child:Container(

                          height:50,
                      width: MediaQuery.of(context).size.width,


                      child: Card(
                        child: Center(
                          child: Row (
                            children : [
                                                 Text(

                          snap[index]['name'],
                          textAlign: TextAlign.start,
                          style: const TextStyle(
                            color: Colors.black54,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                            SizedBox(width:50),
                            Text(

                              snap[index]['title'],
                              textAlign: TextAlign.start,
                              style: const TextStyle(
                                color: Colors.black54,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                          ]


                        )),
                      )),
                        onTap: () {
                     var ur= snap[index]['url'];
                     print( ur );
                     **DashBoards(url: ur);**  //value to be passed


                        },

                      )
                      ],
                //   ),
                 );
              },
            );
          } else {
            return const SizedBox();
          }
        },
      )
    ],

  );

}

heres my second class

class DashBoards extends StatefulWidget {
  final String url;
  const DashBoards({Key? key,required this.url}) : super(key: key);

  @override
  _DashBoardsState createState() => _DashBoardsState(url);
}

class _DashBoardsState extends State<DashBoards> {
  late final String url;
  _DashBoardsState(url);


  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white60,
        endDrawerEnableOpenDragGesture: false,
        appBar: AppBar(
          backgroundColor: Colors.green,
          ),
        body:   WebViewPlus (

      initialUrl: url,
    ),
       );
  }
}

CodePudding user response:

You can access the value by using the widget.

Example:

///First Class
class FirstClass extends StatefulWidget {
  const FirstClass({Key? key}) : super(key: key);

  @override
  State<FirstClass> createState() => _FirstClassState();
}

class _FirstClassState extends State<FirstClass> {
  @override
  Widget build(BuildContext context) {
    return SecondClass(url: "https://www.google.com/");
  }
}

///Second class
class SecondClass extends StatefulWidget {
  final String url;
  const SecondClass({Key? key, required this.url}) : super(key: key);

  @override
  State<SecondClass> createState() => _SecondClassState();
}

class _SecondClassState extends State<SecondClass> {
  @override
  Widget build(BuildContext context) {
    ///Access the value by using widget
    return Text(widget.url);
  }
}
  • Related