How to pass data between widgets in Flutter






Step 1 

class Datainfo {
  final String title;
  final String description;
  final List rating;
  const  Datainfo(this.title, this.description,this.rating);
}

Step 2 

import 'package:flutter/material.dart';
import 'package:props/Datainfo.dart';
import 'package:props/Profile.dart';

void main() {
  runApp(MaterialApp(
    title: "props",
    home: MyHome(),
  ));
}

class MyHome extends StatefulWidget {
  const MyHome({super.key});

  @override
  State<MyHome> createState() => _MyHomeState();
}

class _MyHomeState extends State<MyHome> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ElevatedButton(onPressed: () {
        var userinfo = Datainfo("name", "saddam husain",[1,2,3,45,6]);
        Navigator.push(context, MaterialPageRoute(builder: (context)=>MyProfile(userinfo)));
      },
      child: Text("Click"),
      ),
    );
  }
}

Step 3

import 'package:flutter/material.dart';
import 'package:props/Datainfo.dart';
class MyProfile extends StatelessWidget {
  final Datainfo todos;
MyProfile(this.todos);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("${todos.title} ${todos.description} ${todos.rating.length}"),
    );
  }
}

   child: InkWell(
                      onTap: () {
                      var age = farmerData[index]['age']==null?"N/A":farmerData[index]['age'];
                      var gender = farmerData[index]['gender']=="M"?"Male":"Female";
                      var farmesData =farmerData[index]["farm"];
                      print(farmesData);
                      var userinfo =FarmerData("${farmerData[index]['name']}", "${farmerData[index]['contact_no']}","${farmerData[index]['rating'].length}","${age}","${gender}",farmesData);
                      Navigator.push(
                        context,
                        MaterialPageRoute(builder: (context) => ProfileUI2(userinfo)),
                      );





  final List name;
  Farm(this.name);


Comments

Post a Comment