Skip to content

Instantly share code, notes, and snippets.

@easylive1989
Last active September 17, 2023 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easylive1989/9fdbf6ddf7040af928736c0a004d211d to your computer and use it in GitHub Desktop.
Save easylive1989/9fdbf6ddf7040af928736c0a004d211d to your computer and use it in GitHub Desktop.
2023鐵人賽_D3_1
import 'dart:convert';
import 'package:equatable/equatable.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
///
/// user_repository_test.dart
///
main() {
test("get user ok from api", () async {
var userRepository = UserRepository();
var user = await userRepository.get(1);
expect(user, const User(id: 1, name: "Leanne Graham"));
});
}
///
/// user_repository.dart
///
class UserRepository {
Future<User> get(int userId) async {
var response = await http.get(Uri.parse("https://jsonplaceholder.typicode.com/users/$userId"));
return User.fromJson(jsonDecode(response.body));
}
}
///
/// user.dart
///
class User extends Equatable {
final int id;
final String name;
const User({
required this.id,
required this.name,
});
factory User.fromJson(Map<String, dynamic> json) {
return User(
id: json['id'],
name: json['name'],
);
}
@override
List<Object?> get props => [id, name];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment