Skip to content

Instantly share code, notes, and snippets.

@marcos-bah
Created October 31, 2021 17:58
Show Gist options
  • Save marcos-bah/6552c497fab340034449d68c4647daa4 to your computer and use it in GitHub Desktop.
Save marcos-bah/6552c497fab340034449d68c4647daa4 to your computer and use it in GitHub Desktop.
Exemplo de uso do future
import 'package:flutter/material.dart';
class TesteWidget extends StatefulWidget {
const TesteWidget({Key? key}) : super(key: key);
@override
_TesteWidgetState createState() => _TesteWidgetState();
}
class _TesteWidgetState extends State<TesteWidget> {
Future<List> _future() {
return Future.delayed(const Duration(seconds: 3), () {
return [
{"id": 1, "title": "teste 1"},
{"id": 2, "title": "teste 2"}
];
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("teste"),
centerTitle: true,
),
body: Center(
child: FutureBuilder<List>(
future: _future(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasData && snapshot.hasError == false) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
switch (snapshot.data![index]["id"]) {
case 1:
return ListTile(
title: Text(
"primeiro " + snapshot.data![index]["title"]),
);
case 2:
return ListTile(
title:
Text("segundo " + snapshot.data![index]["title"]),
);
default:
return const ListTile(
title: Text("padrao"),
);
}
},
);
} else {
return const Text("erro");
}
} else {
return const CircularProgressIndicator();
}
},
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment