Flutter 为什么使用定位器链接服务类-颤振?
我试图了解get_it包和定位器的使用 在一篇文章中,我注意到作者使用定位器允许Flutter 为什么使用定位器链接服务类-颤振?,flutter,dart,Flutter,Dart,我试图了解get_it包和定位器的使用 在一篇文章中,我注意到作者使用定位器允许AuthenticationService使用FirestoreService的方法: class AuthenticationService { final FirestoreService _firestoreService = locator<FirestoreService>(); ... 假设您有realFirestoreServiceImplement和MockFires
AuthenticationService
使用FirestoreService
的方法:
class AuthenticationService {
final FirestoreService _firestoreService = locator<FirestoreService>();
...
假设您有real
FirestoreServiceImplement
和MockFirestoreServiceMockImplement
使用
服务定位器
,您可以轻松地注册
模拟
实现如下定位器代码>
在设置定位器中
使用final FirestoreService时_FirestoreService=locator()代码>
您将获得FirestoreServiceMockImplement
而不是FirestoreServiceImplement
代码片段
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
final locator = GetIt.instance;
abstract class FirestoreService {
createUser(User user);
}
class FirestoreServiceImplement extends FirestoreService {
Future createUser(User user) async {}
}
class FirestoreServiceMockImplement extends FirestoreService {
Future createUser(User user) async {}
}
void setupLocator() {
locator.registerLazySingleton<FirestoreService>(
() => FirestoreServiceMockImplement());
}
void main() {
setupLocator();
runApp(MyApp());
}
class User {
final String id;
final String fullName;
final String email;
final String userRole;
User({this.id, this.fullName, this.email, this.userRole});
User.fromData(Map<String, dynamic> data)
: id = data['id'],
fullName = data['fullName'],
email = data['email'],
userRole = data['userRole'];
Map<String, dynamic> toJson() {
return {
'id': id,
'fullName': fullName,
'email': email,
'userRole': userRole,
};
}
}
导入“包装:颤振/材料.省道”;
导入“package:get_it/get_it.dart”;
最终定位器=GetIt.instance;
抽象类FirestoreService{
createUser(用户);
}
类FirestoreServiceImplement扩展了FirestoreService{
未来createUser(用户用户)异步{}
}
类FirestoreServiceMockImplement扩展了FirestoreService{
未来createUser(用户用户)异步{}
}
void setupLocator(){
locator.registerLazySingleton(
()=>FirestoreServiceMockImplement());
}
void main(){
setupLocator();
runApp(MyApp());
}
类用户{
最终字符串id;
最终字符串全名;
最终字符串电子邮件;
最终字符串用户角色;
用户({this.id,this.fullName,this.email,this.userRole});
User.fromData(地图数据)
:id=数据['id'],
fullName=data['fullName'],
电子邮件=数据['email'],
userRole=data['userRole'];
映射到JSON(){
返回{
“id”:id,
“全名”:全名,
“电子邮件”:电子邮件,
“userRole”:userRole,
};
}
}
假设您有realFirestoreServiceImplement
和MockFirestoreServiceMockImplement
使用服务定位器
,您可以轻松地注册
模拟
实现如下定位器代码>
在设置定位器中
使用final FirestoreService时_FirestoreService=locator()代码>
您将获得FirestoreServiceMockImplement
而不是FirestoreServiceImplement
代码片段
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
final locator = GetIt.instance;
abstract class FirestoreService {
createUser(User user);
}
class FirestoreServiceImplement extends FirestoreService {
Future createUser(User user) async {}
}
class FirestoreServiceMockImplement extends FirestoreService {
Future createUser(User user) async {}
}
void setupLocator() {
locator.registerLazySingleton<FirestoreService>(
() => FirestoreServiceMockImplement());
}
void main() {
setupLocator();
runApp(MyApp());
}
class User {
final String id;
final String fullName;
final String email;
final String userRole;
User({this.id, this.fullName, this.email, this.userRole});
User.fromData(Map<String, dynamic> data)
: id = data['id'],
fullName = data['fullName'],
email = data['email'],
userRole = data['userRole'];
Map<String, dynamic> toJson() {
return {
'id': id,
'fullName': fullName,
'email': email,
'userRole': userRole,
};
}
}
导入“包装:颤振/材料.省道”;
导入“package:get_it/get_it.dart”;
最终定位器=GetIt.instance;
抽象类FirestoreService{
createUser(用户);
}
类FirestoreServiceImplement扩展了FirestoreService{
未来createUser(用户用户)异步{}
}
类FirestoreServiceMockImplement扩展了FirestoreService{
未来createUser(用户用户)异步{}
}
void setupLocator(){
locator.registerLazySingleton(
()=>FirestoreServiceMockImplement());
}
void main(){
setupLocator();
runApp(MyApp());
}
类用户{
最终字符串id;
最终字符串全名;
最终字符串电子邮件;
最终字符串用户角色;
用户({this.id,this.fullName,this.email,this.userRole});
User.fromData(地图数据)
:id=数据['id'],
fullName=data['fullName'],
电子邮件=数据['email'],
userRole=data['userRole'];
映射到JSON(){
返回{
“id”:id,
“全名”:全名,
“电子邮件”:电子邮件,
“userRole”:userRole,
};
}
}
我对这方面还不太熟悉,所以我不太了解FirestoreImplement和FirestoreMockImplement之间的区别?Mock表示假,您可以返回假数据,例如用于测试或调试。因此,您不需要在其中放入调试代码,但作者没有找到FirestoreService的模拟版本GetItLocator=GetIt.instance;void setupLocator(){locator.registerLazySingleton(()=>NavigationService());locator.registerLazySingleton(()=>DialogService());locator.registerLazySingleton(()=>AuthenticationService());locator.registerLazySingleton(()=>FirestoreService());}
Author saide:我没有能力在不改变UI的情况下轻松地将业务对象的实现切换为模拟对象。演示代码不会显示所有内容。它只是展示了重要的部分。因为作者来自.Net世界,所以你们可以阅读为什么得到它。我对这一点还不太了解,所以我真的不明白FirestoreImplement和FirestoreMockImplement之间的区别?Mock表示假,您可以返回假数据,例如用于测试或调试。因此,您不需要在其中放入调试代码,但作者没有找到FirestoreService的模拟版本GetItLocator=GetIt.instance;void setupLocator(){locator.registerLazySingleton(()=>NavigationService());locator.registerLazySingleton(()=>DialogService());locator.registerLazySingleton(()=>AuthenticationService());locator.registerLazySingleton(()=>FirestoreService());}
Author saide:我没有能力在不改变UI的情况下轻松地将业务对象的实现切换为模拟对象。演示代码不会显示所有内容。它只是展示了重要的部分。因为作者来自.Net世界,所以你们可以阅读为什么得到它。