How am I able to pass setState in a stateless widget from a statefull widget
4,934
setState()
is a method inside the State
class. In your code you have the following:
onPressed: () {
Demo(function: () {
setState(() {}); //PASSING SET STATE
});
},
Here you create an instance of Demo
and give it a callback as an argument. Inside that callback, you are calling setState
which is a method inside the State
class.
Author by
UTKARSH Sharma
Updated on December 20, 2022Comments
-
UTKARSH Sharma over 1 year
In the below code I have passed set state as a function call in a stateless widget how is this possible I know there is nothing wrong with flutter, but its something about the functions in general, I am not getting the basics right can someone plz explain me this concept. Comments are provided for assistance.
import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { //STATEFUL WIDGET @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { @override Widget build(BuildContext context) { return Scaffold( floatingActionButton: FloatingActionButton( onPressed: () { Demo(function: () { setState(() {}); //PASSING SET STATE }); }, ), ); } } class Demo extends StatelessWidget { //STATE LESS WIDGET final Function function; Demo({this.function}); @override Widget build(BuildContext context) { return Container(); } }
-
Ravindra Kushwaha almost 4 yearsRefer this demo example for passing the value either in State or stateless stackoverflow.com/a/61743422/3946958
-
UTKARSH Sharma almost 4 yearsno, the problem is how can I pass set state call in a stateless widget as stateless widget doesn't know what is set state so how can I pass it as an argument. I know I can pass but (why) is my question
-
pskink almost 4 yearsso is
Demo(function: () { setState(() {}); //PASSING SET STATE });
working or you have any problem with passing such a callback to some other widget? -
UTKARSH Sharma almost 4 yearsit's working fine even I have developed a full-fledged app but the question is (how ).
-
pskink almost 4 yearshave you used any
RaisedButton
/OutlineButton
/FlatButton
? they haveonPressed
property - this is exactly the same - you are passing some function to that button and when the click is performed tha button calls that callback function
-
-
UTKARSH Sharma almost 4 yearsI am not getting the callback thing plz elaborate a bit how it works in this case.
-
Peter Haddad almost 4 years@UTKARSHSharma check this stackoverflow.com/a/59504089/7015400
-
Peter Haddad almost 4 years@UTKARSHSharma @UTKARSHSharma for ex in javascript
function doHomework(subject, callback) { alert(
Starting my ${subject} homework.); callback(); } doHomework('math', function() { alert('Finished my homework'); });
Here, the functiondoHomework
takes a subject variable and function referencecallback
. After the alert appears and you know the subject, then the function (callback) will be called. So basically a callback is a function that is called as a response of something occurring. It can be a response of a click on a button or as a response of retrieving data from the db -
Peter Haddad almost 4 years@UTKARSHSharma so a callback is not called immediately, it is a function that is called in response to something occurring. Thats why its called "callback", you can think about it as call me later. All buttons in flutter, use callback functions and these functions are called in response to when the user clicks the button. All asynchronous code in flutter uses callback and these callbacks are called when the data is retrieved