Pop refresh last screen using Flutter and StreamBuilder [closed] Pop refresh last screen using Flutter and StreamBuilder [closed] dart dart

Pop refresh last screen using Flutter and StreamBuilder [closed]


I guess you would have to use a StreamController instead of a Stream in that example.

From your example, see _streamController occurrences.

class ConfigCNPJPage extends StatefulWidget {  bool voltarTela;  ConfigCNPJPage({Key key, this.voltarTela}) : super(key: key);  @override  ConfigCNPJPageState createState() => new ConfigCNPJPageState();}class ConfigCNPJPageState extends State<ConfigCNPJPage>    with SingleTickerProviderStateMixin {    ResultConfig BD;    List<Map> list;    List<Widget> listArray = [];    StreamController _streamController;  Future setupList() async {    ConfigDatabase db = ConfigDatabase();    var banco = await db.initDB();    list = await banco.rawQuery('SELECT * FROM Empresa');    return list;  }    @override    void initState() {      super.initState();      _streamController = new StreamController();      setupList()?.then((o) => _streamController.add(o));    }  @override  Widget build(BuildContext context) {    return new StreamBuilder(      stream: _streamController.stream,      builder: (BuildContext context, AsyncSnapshot snapshot) {        return snapshot.hasData?  new Scaffold(          resizeToAvoidBottomPadding: false,            appBar: new AppBar(              title: new Text('Configurar Empresa'),              actions: <Widget>[                new IconButton(icon: const Icon(Icons.add), onPressed: () {                  Navigator.pushNamed(context, "NovoCNPJ").then((value) {                      setState(() {                        _streamController.add(value);                      });                  });                })              ],            ),            body: new Column(              children: <Widget>[                  criarLista()              ],            ),        ):new Center(child: new RefreshProgressIndicator());      },);  }}

But in your use case, do you have only ONE empresa/company or will you have many?

If you have only one, instead of having a Stream, it would be way easier to manage just one single _empresa / _company object..

If you expect to have a list of objects, then I wouldn't use a StreamBuilder as well. I would manage a List<Empresa> _empresas as well.

class ConfigCNPJPage extends StatefulWidget {  bool voltarTela;  ConfigCNPJPage({Key key, this.voltarTela}) : super(key: key);  @override  ConfigCNPJPageState createState() => new ConfigCNPJPageState();}class ConfigCNPJPageState extends State<ConfigCNPJPage>    with SingleTickerProviderStateMixin {    ResultConfig BD;    List<Map> list;    List<Widget> listArray = [];    List<Empresa> _empresas;  Future setupList() async {    ConfigDatabase db = ConfigDatabase();    var banco = await db.initDB();    list = await banco.rawQuery('SELECT * FROM Empresa');    return list;  }  @override  void initState() {    super.initState();    setupList()?.then((o) {      if (_empresas == null) _empresas = new List();      // if o is a list, perform a forEach on it      _empresas.add(new Empresa(o))    });  }  @override  Widget build(BuildContext context) {    // o melhor seria usar outra variavel pra controlar o loading,    //   mas deve funcionar assim    return _empresas == null      ? new Center(child: new RefreshProgressIndicator())      : new Scaffold(          resizeToAvoidBottomPadding: false,            appBar: new AppBar(              title: new Text('Configurar Empresa'),              actions: <Widget>[                new IconButton(icon: const Icon(Icons.add), onPressed: () {                  Navigator.pushNamed(context, "NovoCNPJ").then((value) {                      setState(() {                        _empresas.add(new Empresa(value));                      });                  });                })              ],            ),            body: new Column(              children: <Widget>[                  criarLista()              ],            ),        );  }}

Good luck!