How can i call a variable from a state class in flutter?

Viewed 19

I'm trying to call a variable from a state class to another class is that even possible with flutter?

This the var I'm trying to reach:

class PostRec extends StatefulWidget {
  PostRec({
Key? key,
  }) : super(key: key);

  @override
  State<PostRec> createState() => _PostRecState();
}

class _PostRecState extends State<PostRec> {
 
  File? image;

and this is what i tried to do

class previewImg extends StatelessWidget {
  previewImg({Key? key}) : super(key: key);
  File img = _PostRecState.image;

it says that the class is undefined. I also tried to call it using PostRec class and i still get an error.

1 Answers

you can pass it as a property for previewImg class.

class previewImg extends StatelessWidget {
  previewImg({Key? key, this.img}) : super(key: key);
  File? img;

then you can pass it when you call previewImg in another class.

eg: previewImg as a widget child.

Container(
  child: previewImg(img:...your file imafe),
)

or as a screen:

ElevetedButton(
  onTap: (){
    Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => previewImg(
              img: ...your image file,
          ),
       ),
     )
  }
  child:Text("prev img"),
)

now in previewImg class, you can use file as local variable.

Related