Skip to content

Instantly share code, notes, and snippets.

@gupta-shrinath
Created October 4, 2021 07:59
Show Gist options
  • Save gupta-shrinath/71bbc34e43095a97aeb383fd1689d97d to your computer and use it in GitHub Desktop.
Save gupta-shrinath/71bbc34e43095a97aeb383fd1689d97d to your computer and use it in GitHub Desktop.
testWidgets("Show error text widget when future returns error",
(WidgetTester tester) async {
Future<List<Contact>>? future;
final contactsScreen = MaterialApp(
home: buildContactList(
contacts: future,
),
);
await tester.pumpWidget(contactsScreen);
await tester.pumpAndSettle();
final errorContactText = find.text("Oops contact load failed !");
expect(errorContactText, findsOneWidget);
});
Widget buildContactList({required contacts}) {
return FutureBuilder<List<Contact>>(
future: contacts,
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) {
return Card(
margin: EdgeInsets.all(10.0),
child: ListTile(
leading: CircleAvatar(
radius: 40,
backgroundImage: AssetImage("assets/icons/img_avatar.png"),
),
title: Text(snapshot.data![index].getName()),
subtitle: Text(snapshot.data![index].getContactNumber()),
),
);
});
} else if (snapshot.hasError) {
print("haserror");
return Text(
'Oops contact load failed !',
style: TextStyle(
color: Colors.red,
fontSize: 20,
),
);
} else if (snapshot.data == null) {
return Text('No Contacts');
}
return Center(
child: const CircularProgressIndicator(),
);
},
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment