error: setState() called after dispose():

3,035 views
Skip to first unread message

M

unread,
May 31, 2022, 7:42:29 AM5/31/22
to Flutter Development (flutter-dev)


hello 

i have error

   Error: setState() called after dispose(): _addchannelState#86401(lifecycle state: defunct, not mounted)
This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

========

class addchannel extends StatefulWidget {

final uidActiveService;

const addchannel({Key? key, this.uidActiveService}) : super(key: key);

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

class _addchannelState extends State<addchannel> {

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();


List<DropdownMenuItem<String>>? _dropdownMenuItem = [];
String? _activeDropDownItem;

final GlobalKey<FormState> _formKey = GlobalKey();

late String title;
String? streamURL;
@override

void initState() {
// TODO: implement initState
super.initState();
_getServiceData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
iconTheme: IconThemeData(
color: Theme
.of(context)
.primaryColor,
),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Center(
child: Container(
constraints: const BoxConstraints(
maxWidth: 400,
),
child: Column(
children: [
const SizedBox(height: 50,),
Form(
key: _formKey,
child: Column(
children: [
const SizedBox(height: 20,),
TextFormField(
onChanged: (value) => title = value.trim(),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
style: TextStyle(
fontSize: 18,
color: Theme
.of(context)
.colorScheme
.secondary,
),
),
const SizedBox(height: 20,),
TextFormField(
onChanged: (value) => streamURL = value.trim(),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
style: TextStyle(
fontSize: 18,
color: Theme
.of(context)
.colorScheme
.secondary,
),
),
const SizedBox(height: 20,),
DropdownButtonFormField(
items: _dropdownMenuItem,
onChanged: (String? newValue) {
setState(() => _activeDropDownItem = newValue);},
value: _activeDropDownItem,
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 20),
RaisedButton(
color: Theme
.of(context)
.primaryColor,
onPressed: _submitData,
child: Text("ADD",
style: TextStyle(color: Theme
.of(context)
.canvasColor,))
),
],
),
),
],
),
),
)
],
),
),
),
);
}

_getServiceData() async {

List<SectionModel> services = await FirebaseManager.shared.getAllSection().first;

setState(() {
_dropdownMenuItem = services.map((item) => DropdownMenuItem(child: Text(item.title ), value: item.uid.toString())).toList();

if (widget.uidActiveService != null) {
_activeDropDownItem = widget.uidActiveService;
}

});

}


bool _validation() {
return !(_activeDropDownItem == null || title == "" );
}

_submitData() async {
if (!_validation()) {
_scaffoldKey.showTosta(
message: "Please fill in all fields", isError: true);
return;
}

await FirebaseManager.shared.getSectionById(id: _activeDropDownItem!).first;

FirebaseManager.shared.addChannel(
context,
sectionuid: _activeDropDownItem!,
title: title,
streamURL: streamURL!,
);
}

class addchannel extends StatefulWidget {

final uidActiveService;

const addchannel({Key? key, this.uidActiveService}) : super(key: key);

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

class _addchannelState extends State<addchannel> {

GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();


List<DropdownMenuItem<String>>? _dropdownMenuItem = [];
String? _activeDropDownItem;

final GlobalKey<FormState> _formKey = GlobalKey();

late String title;
String? streamURL;
@override

void initState() {
// TODO: implement initState
super.initState();
_getServiceData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
iconTheme: IconThemeData(
color: Theme
.of(context)
.primaryColor,
),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Center(
child: Container(
constraints: const BoxConstraints(
maxWidth: 400,
),
child: Column(
children: [
const SizedBox(height: 50,),
Form(
key: _formKey,
child: Column(
children: [
const SizedBox(height: 20,),
TextFormField(
onChanged: (value) => title = value.trim(),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
style: TextStyle(
fontSize: 18,
color: Theme
.of(context)
.colorScheme
.secondary,
),
),
const SizedBox(height: 20,),
TextFormField(
onChanged: (value) => streamURL = value.trim(),
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
style: TextStyle(
fontSize: 18,
color: Theme
.of(context)
.colorScheme
.secondary,
),
),
const SizedBox(height: 20,),
DropdownButtonFormField(
items: _dropdownMenuItem,
onChanged: (String? newValue) {
setState(() => _activeDropDownItem = newValue);},
value: _activeDropDownItem,
autovalidateMode: AutovalidateMode.onUserInteraction,
),
const SizedBox(height: 20),
RaisedButton(
color: Theme
.of(context)
.primaryColor,
onPressed: _submitData,
child: Text("ADD",
style: TextStyle(color: Theme
.of(context)
.canvasColor,))
),
],
),
),
],
),
),
)
],
),
),
),
);
}

_getServiceData() async {

List<SectionModel> services = await FirebaseManager.shared.getAllSection().first;

setState(() {
_dropdownMenuItem = services.map((item) => DropdownMenuItem(child: Text(item.title ), value: item.uid.toString())).toList();

if (widget.uidActiveService != null) {
_activeDropDownItem = widget.uidActiveService;
}

});

}


bool _validation() {
return !(_activeDropDownItem == null || title == "" );
}

_submitData() async {
if (!_validation()) {
_scaffoldKey.showTosta(
message: "Please fill in all fields", isError: true);
return;
}

await FirebaseManager.shared.getSectionById(id: _activeDropDownItem!).first;

FirebaseManager.shared.addChannel(
context,
sectionuid: _activeDropDownItem!,
title: title,
streamURL: streamURL!,
);
}

M

unread,
May 31, 2022, 7:49:30 AM5/31/22
to Flutter Development (flutter-dev)
 kindly check video 


_getServiceData() async {

List<SectionModel> services = await FirebaseManager.shared.getAllSection().first;

if (mounted) {
setState(() {
_dropdownMenuItem = services.map((item) => DropdownMenuItem(value: item.uid.toString(), child: Text(item.title ))).toList();
Screen Recording 1443-11-01 at 2.47.18 PM.mov
Reply all
Reply to author
Forward
0 new messages