Home > other >  How can i achieve sending data from `async` class to another page this in flutter?
How can i achieve sending data from `async` class to another page this in flutter?

Time:10-03

I want to pass 'data' from connectDevice asynchronous function to screen DeviceInformation(), what should I do ?

Screen 1: Source of data

Future<void> _connectDevice(Data data) async {
    var connectRequest = {
      'serialNumber': data.serialNumber,
      'modelName': data.modelName,
      'ipAddr': data.ipAddr,
    };

    var apiEndpoint = VNPTTechAPI.shared;
    var res = await apiEndpoint.connectDevice(connectRequest);
    print(res);
    print('------------$connectRequest');
    print(data.ipAddr);
    //Navigator.pushNamed(context, 'deviceInformation');
    Navigator.pushNamed(context, 'deviceInformation', arguments: {
      'serialNumber': data.serialNumber,
      'modelName': data.modelName,
      'ipAddr': data.ipAddr,
      'deviceMac': data.deviceMac,
    });}

screen 2: Receiving end

class DeviceInformation extends StatefulWidget {
  const DeviceInformation({Key? key}) : super(key: key);

  @override
  State<DeviceInformation> createState() => _DeviceInformationState();
}

class _DeviceInformationState extends State<DeviceInformation> {
  @override
  Widget build(BuildContext context) {
    return Material();

CodePudding user response:

in your receiving end do this in your build

@override
  Widget build(BuildContext context) {
... 
   final getData = _connectDevice(`your data value`);
...
  ///Create a futurebuilder to receive snapshot of your data
   child: FutureBuilder(){
   future: getData,
   ....
     if (snapshot.hasData) {
     //do your magic here 
     }else{
     return CircularProgressIndicator();
     }


This will help Document

CodePudding user response:

you can use Future Builder Like this

 Widget getBody(BuildContext context) {
      return SizedBox(
       height: MediaQuery.of(context).size.height,
       child: FutureBuilder<List<Person>?>(
       future: getPerson(),
         builder: (BuildContext context, AsyncSnapshot<List<Person>?> snapshot) {
        if (snapshot.hasData == false) {
          return Text("Loding .....");
        }
        if (snapshot.data!.isEmpty == true) {
          return Center(
            child: Container(
              alignment: Alignment.center,
              child: const Text(
              "No Data",
                textAlign: TextAlign.center,
                style: TextStyle(color: accent, fontWeight: FontWeight.bold),
              ),
            ),
          );
        }
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return loderList();

            // ignore: dead_code
            break;

          default:
            return ListView.builder(
              itemCount: snapshot.data!.length,
              itemBuilder: (context, i) {
                return YourWidget(
                  id: snapshot.data![i].id!,
                  name: snapshot.data![i].name,
                  location: snapshot.data![i].location,
                );
              },
            );
            // ignore: dead_code
            break;
        }
      },
    ),
  );
}




class YourWidget extends StatelessWidget {
  int? id;
  String? name, location;
  YourWidget ({this.id, this.name, this.location});

  @override
  Widget build(BuildContext context) {
    return Card(
      color: black,
      shadowColor: grey,
      elevation: 1,
      margin: const EdgeInsets.all(8),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
      child: ListTile(
        isThreeLine: true,
        dense: true,
        title: Text(
          name!,
          style: const TextStyle(
              color: white, fontWeight: FontWeight.bold, fontSize: 16),
        ),
        subtitle: Text(
          location!,
          style: const TextStyle(
              color: grey, fontWeight: FontWeight.bold, fontSize: 14),
        ),
        leading: const Icon(
          Icons.location_on_sharp,
          color: accent,
        ),
      ),
    );
  }
}

CodePudding user response:

You can receive data from first screen in Map DataType and collect that data like this...

class DeviceInformation extends StatefulWidget {
  const DeviceInformation({Key? key}) : super(key: key);

  @override
  State<DeviceInformation> createState() => _DeviceInformationState();
}

class _DeviceInformationState extends State<DeviceInformation> {
  @override
  Widget build(BuildContext context) {
    var argumentData = ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>;
    print("argumentData : ${argumentData}");
    print("serialNumber : ${argumentData['serialNumber']}");
    print("modelName : ${argumentData['modelName']}");
    return Material();
  }
}
  • Related