Instantiating a method in another class

Asked

Viewed 1,152 times

0

I am instantiating a method of another class, in which it uses a list filled by initState, but when entering the method I am calling, the list comes empty, as if it had not been executed, I’m posting the two classes so you can help me.

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:helpdesk_app/tabs/chamados_interacoes_tab.dart';

class ChamadosDetalhe extends StatefulWidget {

final int chamadoId;
final String titulo;
final String conteudo;
final String data;
final String usuarioInclusao;
final String responsavel;
final String laboratorio;
final String prioridade;


ChamadosDetalhe(this.chamadoId,this.titulo,this.conteudo,
this.data,this.usuarioInclusao,this.responsavel,this.laboratorio,this.prioridade);

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

class _ChamadosDetalheState extends State<ChamadosDetalhe> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 20,
        child: Scaffold(
          appBar: AppBar(
            title: Text("Chamado ${this.widget.chamadoId.toString()} - ${this.widget.laboratorio}", overflow: TextOverflow.fade,),
            centerTitle: true,
            actions: <Widget>[
                PopupMenuButton<OrderOptions>(
                 itemBuilder: (context) => <PopupMenuEntry<OrderOptions>>[
                  const PopupMenuItem<OrderOptions>(
                    child: Text("Ordernar Mais Antigas"),
                    value: OrderOptions.orderAsc,
                  ),
                  const PopupMenuItem<OrderOptions>(
                    child: Text("Ordernar Mais Recentes"),
                    value: OrderOptions.orderDesc,                    
                  )
            ],
           onSelected : (context){      ChamadoInteracao(this.widget.chamadoId,idChamado:      this.widget.chamadoId).createState().orderList(OrderOptions.orderAsc);
             setState(() {

             });//Aqui é onde estou chamando o orderList da classe no outro código
           }            
          ),
        ],
          ),
          body:  ListView(
              children: <Widget>[
               Card(
                  child: Column(
                    children: <Widget>[
                      Text("${this.widget.titulo}",
                      style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold),
                      maxLines: 3,
                      overflow: TextOverflow.clip,
                      textAlign: TextAlign.center,
                       ),
                       Divider(),
                       Text("${this.widget.usuarioInclusao}", style: TextStyle(fontWeight: FontWeight.bold),),
                       Text("${this.widget.data}"),
                       Divider(),
                       Text("${this.widget.conteudo}",
                       textAlign: TextAlign.start,
                       )
                    ],
                  ),
                ),
                Divider(),                
                ChamadoInteracao(this.widget.chamadoId,idChamado: this.widget.chamadoId),
                Divider(),
                Card(
                  child: Column(
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Text("Responsável - ",style: TextStyle(fontWeight: FontWeight.bold),
                            textAlign: TextAlign.start,),
                          Text("${this.widget.responsavel}",overflow: TextOverflow.fade),
                          Text(" Prioridade -  ",style: TextStyle(fontWeight: FontWeight.bold),
                            textAlign: TextAlign.start),
                          Text("${this.widget.prioridade}",overflow: TextOverflow.fade),
                        ],
                      )
                    ],
                  ),
                ),
                Divider(),
              ],
            ), 
        ),
    );
  }

}

Class that is instantiated below

import 'package:flutter/material.dart';
import 'package:helpdesk_app/models/chamado_interacao_model.dart';
import 'package:helpdesk_app/repository/chamado_repository.dart';
import 'package:helpdesk_app/tiles/chamados_interacoes_tile.dart';


 enum OrderOptions {orderDesc, orderAsc}
class ChamadoInteracao extends StatefulWidget {

final int idChamado;  
 const ChamadoInteracao(int id, {Key key, this.idChamado}) : super(key:key);

  @override
  _ChamadoInteracaoState createState() => _ChamadoInteracaoState();
}
class _ChamadoInteracaoState extends State<ChamadoInteracao> {
  List<ChamadosInteracao> _chamadoInteracao = <ChamadosInteracao>[];    

  @override
  void initState(){
    super.initState();
    listenForChamadosInteracoes();     
  }  

  listenForChamadosInteracoes() async{
    final Stream<ChamadosInteracao> stream = await getInteracoes(widget.idChamado);
    stream.listen((ChamadosInteracao chamadoInteracao) => setState(()=> _chamadoInteracao.add(chamadoInteracao)));
  } 

  _fetchEntry(int index) async  {
    await Future.delayed(Duration(milliseconds: 500));
  }

  @override
  Widget build(BuildContext context) {  
    return ListView.builder(                  
          shrinkWrap: true,
          physics: ScrollPhysics(),
          itemCount: _chamadoInteracao.length,
          itemBuilder: (context,index){
            return FutureBuilder(
              future: _fetchEntry(index),
              builder: (context,snapshot){
                switch (snapshot.connectionState){
                  case ConnectionState.none:
                  case ConnectionState.waiting:
                  return Center(
                    child:CircularProgressIndicator(
                      backgroundColor: Colors.black12));
                  case ConnectionState.done:
                  default:
                  if(snapshot.hasError){
                    return Text("Erro ${snapshot.error}");
                  }else{                       
                    return ChamadoInteracaoTile(_chamadoInteracao[index]);                    
                  }
                }
              },
            );
          }           
    );
  }   

  void orderList(OrderOptions result) { 
      initState();
      switch(result){
        case OrderOptions.orderAsc:
             _chamadoInteracao.sort((a,b) {
              return a.dataInteracao.compareTo(b.dataInteracao);
          });
          break;
        case OrderOptions.orderDesc:
            _chamadoInteracao.sort((a,b){
              return b.dataInteracao.compareTo(a.dataInteracao);
          });          
          break;          
      }      

  }
}

inserir a descrição da imagem aqui

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.