-1
Communication between screens is essential in an application.
In this case I am trying to conduct a search from a Searchdelegate
But when I return from the Searchdelegate I cannot communicate with the internal tab
For example:
Home.Dart
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
TabController _tabController;
List<String> _itensMenu = ["Sair"];
_escolherMenuItem(String itemEscolhido) {
switch (itemEscolhido) {
case "Sair":
_sairDoApp();
}
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () async {
String res = await showSearch(
context: context, delegate: CustomSearchDelegate());
AbaProdutos().pesquisar(res);
}),
PopupMenuButton(
onSelected: _escolherMenuItem,
itemBuilder: (context) {
return _itensMenu.map((String item) {
return PopupMenuItem<String>(value: item, child: Text(item));
}).toList();
})
],
)
)
}
}
Notice that I am calling the following function: AbaProdutos().pesquisar(res)
Abaprodutos.Dart
class AbaProdutos extends StatefulWidget {
pesquisar(param) => createState().pesquisar(param);
@override
_AbaProdutosState createState() => _AbaProdutosState();
}
class _AbaProdutosState extends State<AbaProdutos> {
pesquisar(param) {
print("pesquisar param " + param);
if (this.mounted) {
print("montado ");
setState(() {
_strPesquisar = param;
_paginaAtual = 1;
_obterProdutos();
});
} else {
print("não montado");
}
}
}
I can even get to this function. I’m doing the test if it’s mounted.
But it never gets inside the if( this.mounted )
And when I take the if( this.mounted )
he makes the following mistake
E/flutter (32036): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: setState() called in constructor: _Abaprodutosstate#06c34(Lifecycle state: created, no widget, not Mounted) E/flutter (32036): This Happens when you call setState() on a State Object for a widget that hasn’t been inserted into the widget Tree yet. It is not necessary to call setState() in the constructor, Since the state is already assumed to be Dirty when it is initially created.
The question:
How to make the Home search function work within the tab without problems?
Or even if there’s a way I keep hearing changes to the home variable inside the tab