Skip to content

Instantly share code, notes, and snippets.

@allansrc
Last active August 14, 2019 15:01
Show Gist options
  • Save allansrc/b9ffadcd2ef5ba5af009ac91e0c90ea1 to your computer and use it in GitHub Desktop.
Save allansrc/b9ffadcd2ef5ba5af009ac91e0c90ea1 to your computer and use it in GitHub Desktop.
try future
// função future
Future<List<Att>> _pegaAtt() async {
var dados = await socResp();
var attDados;
if(dados is !Map){
attDados = json.decode(dados);
}
List<Att> atts = [];
for (var a in attDados){
Att att = Att(
a['usuario_id'],
a['remetente_id'],
a['tronco'],
a['avatar'],
a['tipo']
);
atts.add(att);
}
return atts;
}
//future builder
/**
FutureBuilder(
future: _pegaAtt(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData == false){
return Container(
child: Center(
child: Text('Não há nada pra mostrar . . .')));
} else {
return ListView.builder(
reverse: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
var att = snapshot.data[index];
return Container()
}
)
}
}
);
*/
// função de await usada
socResp() async {
var sAtt = respAtendimento;
return sAtt;
}
// socket onde chamo a func acima
io.on("atendimentoBegin", (data) {
respAtendimento.add(data);
socResp();
print(respAtendimento);
pprint("atendimentoBegin");
pprint(data);
});
>(ConnectionState.waiting, null, null)
>(ConnectionState.done, [], null)
(ConnectionState.waiting, [{usuario_id: 12, remetente_id: 22, tronco: 1167,nome: Novo,telefone: 551199,avatar: https://pps.app./v/t61.24694-24642376431_35n.jpg?ab, protocolo: 201988211520,
...
>(ConnectionState.done, [{usuario_id: 12, remetente_id: 22, tronco: [...]
body: Container(child: Column(mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: FutureBuilder(
// future: attSock(),
future: attSock(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData == false){
return Container(
child: Center(
child: Text('Não há nada no momento . . .'),
)
);
} else {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return LinearProgressIndicator();
case ConnectionState.done:
case ConnectionState.active:
return ListView.builder(
reverse: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
var att = snapshot.data[index];
return Container(
margin: EdgeInsets.only(bottom: 2.0),
color: Colors.lightBlue[50],
// elemento com Avatar > nome > telefone (cliente)
child: ListTile(
leading: CircleAvatar(
backgroundImage: NetworkImage(att['avatar']),
backgroundColor: Colors.transparent,
),
title: Text(att['cliente_nome']),
subtitle: Text(att['cliente_telefone']),
trailing: Text(att['tronco']),
onTap: () {
// soc.getMensagens(
getMensagens(
att['id'],
att['remetente_id'],
);
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return BubbleAtendimento(
dados: dadosUsuario, // dado
avatarCliente: att['avatar'], //avaatar
nomeCliente: att['nome'],
telefoneCliente: att['telefone'],
troncoCliente: att['tronco'],
atendimento: att,
);
}));
}));
});
}
}})
),
Divider(
height: 3.0,
)])));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment