Search code examples
flutterclassdartsetzip

how can i call setState from difference class


FLUTER DART

I have two files like this

one is stful class like this

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

  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: () {
        updateField() // here i call function from other page
      },
      child: null,

    );
  }
}

and one is function into other file or page like this

  updateField()async{
    FirebaseFirestore.instance.collection("users").doc(currentUser.uid).update({
      "faceType" : currentUser.faceType ,
    });
  setState(() {
      currentUser.faceType= faceType;
    });
  }

but once i use setstate it says setstate is not defined , how can i use it please thanks


Solution

  • Make sure you're calling setState within the stateful widget.

    The setState is a method available within only Stateful widgets. Anyone outside the stateful widget is just a custom method you wrote.

    That being said you can use a state management library of your choice and you'll be able to easily change different states within your stateful widget.

    The best part is that the logic code would not have to be within the UI.