【问题标题】:Flutter Getx - "Xxx" not found. You need to call "Get.put(Xxx())" - But i have called Get.put(Xxx())Flutter Getx - 未找到“Xxx”。您需要调用“Get.put(Xxx())” - 但我已调用 Get.put(Xxx())
【发布时间】:2021-02-26 14:21:25
【问题描述】:

我有这个全局绑定类来初始化一些服务,我需要立即初始化它:

import 'package:get/get.dart';
import 'package:vepo/data/data_provider/local_data_provider.dart';
import 'package:vepo/data/data_source/local_data_source.dart';

import 'services/authentication_service.dart';

class GlobalBindings extends Bindings {
  final LocalDataProvider _localDataProvider = LocalDataProvider();
  @override
  void dependencies() {
    Get.put<AuthenticationService>(AuthenticationService(), permanent: true);
    Get.put<LocalDataProvider>(_localDataProvider, permanent: true);
    Get.put<LocalDataSource>(LocalDataSource(_localDataProvider),
        permanent: true);
  }
}

在我的初始绑定中:

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Vepo',
      initialRoute: AppPages.INITIAL,
      initialBinding: GlobalBindings(),
      transitionDuration: const Duration(milliseconds: 500),
      defaultTransition: Transition.rightToLeft,
      getPages: AppPages.routes,
      home: Root(),
      theme: homeTheme,
    );
  }
}

然后在类构造函数中我尝试“找到”它:

class UserLocalRepository extends VpService implements IUserLocalRepository {
  UserLocalRepository() {
    localDataSource = Get.find<LocalDataSource>();
  }

  LocalDataSource localDataSource;

并得到这个错误:

══════ Exception caught by widgets library ═══════════════════════════════════
The following message was thrown building App(dirty):
"LocalDataSource" not found. You need to call "Get.put(LocalDataSource())" or "Get.lazyPut(()=>LocalDataSource())"

The relevant error-causing widget was
App
lib/main.dart:17
When the exception was thrown, this was the stack
#0      GetInstance.find
package:get/…/src/get_instance.dart:272
#1      Inst.find
package:get/…/src/extension_instance.dart:66
#2      new UserLocalRepository
package:vepo/…/user/user_local_repository.dart:10
#3      new LoggedOutNickNameBinding
package:vepo/…/logged_out_nickname/logged_out_nick_name_binding.dart:11
#4      AppPages.routes
package:vepo/…/routes/app_pages.dart:29
...
════════════════════════════════════════════════════════════════════════════════

这是错误信息中提到的绑定:

class LoggedOutNickNameBinding extends Bindings {
  LoggedOutNickNameBinding() {
    _repository = Get.put(UserLocalRepository());
  }

  IUserLocalRepository _repository;

  @override
  void dependencies() {
    Get.lazyPut<LoggedOutNickNameController>(
      () => LoggedOutNickNameController(_repository),
    );
  }
}

为什么“initialBindings”没有初始化,以便我的应用在启动时可以“找到”它们?

【问题讨论】:

    标签: flutter dart flutter-get flutter-getx


    【解决方案1】:

    我猜你的 GlobalBindings.dependencies() 方法被调用的时间和你需要这些资源的时间和顺序不匹配。

    您可以尝试将 Bindings 类之前初始化为 GetMaterialApp,而不是将 Bindings 类传递给GetMaterialApp。

    void main() async {
      //WidgetsFlutterBinding.ensureInitialized(); // uncomment if needed for resource initialization
      GlobalBindings().dependencies();
      runApp(MyApp());
    }
    

    切线

    只是在这里猜测,但是您通过 Get.put 初始化的某些类在准备好使用之前启动缓慢(即异步)?

    如果是这样,你可以使用

    Get.putAsync<YourClass>(() async {
     // init YourClass here
     return await YourClass.slowInit();
    
    }
    

    示例

    我最近进行了一项练习,即在加载应用程序以供用户交互之前执行异步绑定初始化。代码如下:

    import 'package:flutter/material.dart';
    import 'package:get/get.dart';
    
    enum Version {
      lazy,
      wait
    }
    // Cmd-line args/Env vars: https://stackoverflow.com/a/64686348/2301224
    const String version = String.fromEnvironment('VERSION');
    const Version running = version == "lazy" ? Version.lazy : Version.wait;
    
    void main() async {
      //WidgetsFlutterBinding.ensureInitialized(); // if needed for resources
      if (running == Version.lazy) {
        print('running LAZY version');
        LazyBindings().dependencies();
      }
    
      if (running == Version.wait) {
        print('running AWAIT version');
        await AwaitBindings().dependencies(); // await is key here
      }
    
      runApp(MyApp());
    }
    
    class LazyBindings extends Bindings {
      @override
      void dependencies() {
        Get.lazyPut<MyDbController>(() => MyDbController());
      }
    }
    
    /// Simulates a slow (2 sec.) init of a data access object.
    /// Calling [await] dependencies(), your app will wait until dependencies are loaded.
    class AwaitBindings extends Bindings {
      @override
      Future<void> dependencies() async {
        await Get.putAsync<MyDbController>(() async {
          Dao _dao = await Dao.createAsync();
          return MyDbController(myDao: _dao);
        });
      }
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatelessWidget {
      final MyDbController dbc = Get.find();
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('GetX Bindings'),
          ),
          body: Center(
            child: Obx(() => Text(dbc.dbItem.value)),
          ),
        );
      }
    }
    
    class MyDbController extends GetxController {
      Dao myDao;
    
      MyDbController({this.myDao});
    
      RxString dbItem = 'Awaiting data'.obs;
    
      @override
      void onInit() {
        super.onInit();
        initDao();
      }
    
      Future<void> initDao() async {
        // instantiate Dao only if null (i.e. not supplied in constructor)
        myDao ??= await Dao.createAsync();
        dbItem.value = myDao.dbValue;
      }
    }
    
    class Dao {
      String dbValue;
    
      Dao._privateConstructor();
    
      static Future<Dao> createAsync() async {
        var dao = Dao._privateConstructor();
        print('Dao.createAsync() called');
        return dao._initAsync();
      }
    
      /// Simulates a long-loading process such as remote DB connection or device
      /// file storage access.
      Future<Dao> _initAsync() async {
        dbValue = await Future.delayed(Duration(seconds: 2), () => 'Some DB data');
        print('Dao._initAsync done');
        return this;
      }
    }
    
    

    【讨论】:

      【解决方案2】:

      在我的情况下 ::::

      TestCartController? cartController;
      
      if(condition){
       cartController = Get.isRegistered<TestCartController>()
                  ? Get.find<TestCartController>()
                  : Get.put(TestCartController());
      
      }
      

      但在其他一些小部件中,我将上述控制器称为

      final cartController = Get.find<TestCartController>();
      

      类型不匹配问题因为两个实例不同,所以这给我带来了问题。我只是删除了那个?标记并成功了。

      TestCartController cartController;
      
      if(condition){
       cartController = Get.isRegistered<TestCartController>()
                  ? Get.find<TestCartController>()
                  : Get.put(TestCartController());
      
      }
      

      【讨论】:

        【解决方案3】:

        原因:发生在Get.put(Controller)之前调用Get.find()。在初始化 Get.put() 之前调用 Get.find() 显示错误

        解决方案:您只需将 Get.put(controller) 调用到您的主类中,如下所示。那么任何类的 Get.find() 都会得到它。

        void main() {
          runApp(MyApp());
        }
        
        class MyApp extends StatefulWidget {
        
          @override
          _MyAppState createState() => _MyAppState();
        }
        
        class _MyAppState extends State<MyApp> {
          final YourController controller = Get.put(YourController());
          ...
          
          @override
          Widget build(BuildContext context) {
          
            return MaterialApp(
            ......
          
        }
        

        【讨论】:

          猜你喜欢
          • 2021-09-28
          • 2021-11-05
          • 2021-12-27
          • 2022-12-16
          • 1970-01-01
          • 1970-01-01
          • 2013-01-23
          • 2012-11-20
          • 2023-03-10
          相关资源
          最近更新 更多