text stringlengths 1 474 |
|---|
// then parse the JSON. |
return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>); |
} else { |
// If the server did not return a 200 OK response, |
// then throw an exception. |
throw Exception('Failed to load album'); |
} |
} |
class Album { |
final int userId; |
final int id; |
final String title; |
const Album({required this.userId, required this.id, required this.title}); |
factory Album.fromJson(Map<String, dynamic> json) { |
return Album( |
userId: json['userId'] as int, |
id: json['id'] as int, |
title: json['title'] as String, |
); |
} |
} |
void main() => runApp(const MyApp()); |
class MyApp extends StatefulWidget { |
const MyApp({super.key}); |
@override |
State<MyApp> createState() => _MyAppState(); |
} |
class _MyAppState extends State<MyApp> { |
late final Future<Album> futureAlbum; |
@override |
void initState() { |
super.initState(); |
futureAlbum = fetchAlbum(http.Client()); |
} |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
title: 'Fetch Data Example', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: Scaffold( |
appBar: AppBar( |
title: const Text('Fetch Data Example'), |
), |
body: Center( |
child: FutureBuilder<Album>( |
future: futureAlbum, |
builder: (context, snapshot) { |
if (snapshot.hasData) { |
return Text(snapshot.data!.title); |
} else if (snapshot.hasError) { |
return Text('${snapshot.error}'); |
} |
// By default, show a loading spinner. |
return const CircularProgressIndicator(); |
}, |
), |
), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>test/fetch_album_test.dart |
<code_start>import 'package:flutter_test/flutter_test.dart'; |
import 'package:http/http.dart' as http; |
import 'package:mocking/main.dart'; |
import 'package:mockito/annotations.dart'; |
import 'package:mockito/mockito.dart'; |
import 'fetch_album_test.mocks.dart'; |
// Generate a MockClient using the Mockito package. |
// Create new instances of this class in each test. |
@GenerateMocks([http.Client]) |
void main() { |
group('fetchAlbum', () { |
test('returns an Album if the http call completes successfully', () async { |
final client = MockClient(); |
// Use Mockito to return a successful response when it calls the |
// provided http.Client. |
when(client |
.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'))) |
.thenAnswer((_) async => |
http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200)); |
expect(await fetchAlbum(client), isA<Album>()); |
}); |
test('throws an exception if the http call completes with an error', () { |
final client = MockClient(); |
// Use Mockito to return an unsuccessful response when it calls the |
// provided http.Client. |
when(client |
.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1'))) |
.thenAnswer((_) async => http.Response('Not Found', 404)); |
expect(fetchAlbum(client), throwsException); |
}); |
}); |
}<code_end> |
<topic_end> |
<topic_start> |
Summary |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.