seyyed javad
seyyed javad

Reputation: 43

how to receive data in constructor again in flutter?

I have a button in a page. and a class named MySdClass. In my main class i have a constructor that receive some data from MySdClass. i want wen user click on button the constructor of that page receive data from MySdClass again. The code I imagine in my mind does not work:

import 'package:flutter/material.dart';
import 'package:./SdClass.dart';

void main(){
runApp(MainClass(inputData: MySdClass.data,));
}

class MainClass extends StatelessWidget {
final String data;
const MainClass({Key? key, required this.data}) : super(key: key);

@override
Widget build(BuildContext context) {
return Row(
  children: [
    Container(
      const Text(this.widget.data),
    ),
    ElevatedButton(onPressed: (){//what should be here?}, child: const 
Text('next')),
     ],
   );
  }
 }

Upvotes: 0

Views: 474

Answers (1)

Ali Hassan
Ali Hassan

Reputation: 731

void main(){
  runApp(MainClass(inputData: MySdClass.data, onPressed:(){
  "Perform some action and you can send data again through this contructor"
});
 }

 class MainClass extends StatelessWidget {
 final String data;
 void Function() onPressed;
 MainClass({Key? key, required this.data,required this.onPressed}) : super(key: key);

  @override
 Widget build(BuildContext context) {
 return Row(
  children: [
Container(
  const Text(this.widget.data),
),
ElevatedButton(onPressed: onPressed, child: const 
 Text('next')),
 ],
  );
 }
     }

Upvotes: 2

Related Questions