调用AuthController时出现GetX依赖未找到异常,求解决方案
解决GetX中"dynamic" not found异常
问题原因
代码中调用Get.find()时未指定泛型类型,导致GetX无法准确识别要获取的依赖实例,从而抛出"dynamic" not found异常。同时main方法存在语法错误,会影响依赖初始化的执行。
修复步骤
给所有Get.find()添加明确泛型类型
修改初始化函数中的依赖获取代码,确保每个Get.find()都指定对应类型:Future init() async { // Core final sharedPreferences = await SharedPreferences.getInstance(); Get.lazyPut(() => sharedPreferences); Get.lazyPut(() => ApiClient( appBaseUrl: AppConstants.BASE_URL, )); // Repository Get.lazyPut(() => AuthRepo( apiClient:Get.find<ApiClient>(), sharedPreferences: Get.find<SharedPreferences>() )); // controller Get.lazyPut(() => AuthController(authRepo: Get.find<AuthRepo>())); }修正main方法的语法错误
main方法中runApp的参数格式错误,正确写法如下:void main() async{ await di.init(); runApp(MyApp()); }调用AuthController的代码无需修改
你的调用代码已正确指定泛型,保持原样即可:if (Get.find<AuthController>().isLoggedIn()) { //statements }
内容的提问来源于stack exchange,提问作者chubi
相关产品推荐
相关产品推荐

