Flutter GetIt插件-GetIt中没有注册xxx类型

xwbd5t1u  于 11个月前  发布在  Flutter
关注(0)|答案(5)|浏览(163)

我按照示例项目中所示设置了所有内容:

import 'package:get_it/get_it.dart';
import 'package:places/services/authService.dart';

final locator = GetIt.instance;

void setupLocator() {
  locator.registerSingleton<AuthService>(AuthService());
  print("registered");
}

字符串
在主文件中调用

void main() {
  setupLocator();
  runApp(MyApp());
}


我有一些检查定位器也正确返回我的AuthService

class AuthGuardView extends StatefulWidget {
  AuthGuardView({Key key}) : super(key: key);

  @override
  _AuthGuardViewState createState() => _AuthGuardViewState();
}

class _AuthGuardViewState extends State<AuthGuardView> {
  @override
  Widget build(BuildContext context) {
    return ViewModelProvider<AuthGuardViewModel>.withConsumer(
      viewModel: AuthGuardViewModel(),
      onModelReady: (model) => model.initialise(),
      builder: (context, model, child) => model.isLoggedIn
          ? Container(
              color: Colors.white,
              child: Text("Logged In"),
            )
          : SignUpView(),
    );
  }
}

class AuthGuardViewModel extends ChangeNotifier {
  AuthService _authService = locator<AuthService>();
  bool isLoggedIn = false;

  void initialise() async {
    isLoggedIn = await _authService.isLoggedIn();
    notifyListeners();
  }
}


如果我在SignUpView的ViewModel中做同样的事情,我会得到以下错误

flutter: The following assertion was thrown building SignUpView(dirty, state: _SignUpViewState#01129):
flutter: No type AuthService is registered inside GetIt.
flutter:  Did you forget to pass an instance name?
flutter: (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you
flutter: forget to register it?)
flutter: 'package:get_it/get_it_impl.dart':
flutter: Failed assertion: line 248 pos 14: 'instanceFactory != null'


在AuthGuard的ViewModel中,我成功地检索到了auth服务。我还注解掉了定位器代码(因为我认为它可能是auth c调用或类似的东西),但同样的错误仍然存在。
我正在使用get_it:^4.0.1,但降级到3.x.x时错误仍然存在


的数据
在这里,SignUpViewModel

class SignUpViewModel extends ChangeNotifier {
  SignUpViewModel(){
    if(locator.isRegistered<AuthService>()) {
      AuthService _authService = locator<AuthService>();
    } 
  }
  var textInputFormatter = [
    WhitelistingTextInputFormatter(RegExp(r'\d')),
    PhoneNumberTextInputFormatter()
  ];
  var textEditingController;
  var context;
}

58wvjzkj

58wvjzkj1#

当要注册为singleton的类有singleton方法时会发生这种情况。要解决这个问题,您需要在运行runApp()之前等待单例完全生成。

void main() async {

/*  WidgetsFlutterBinding.ensureInitialized() is required in Flutter v1.9.4+ 
 *  before using any plugins if the code is executed before runApp. 
 */
  WidgetsFlutterBinding.ensureInitialized();

// Configure injecction
   await setupLocator();

   runApp(MyApp());
}

字符串

kq0g1dla

kq0g1dla2#

加上这个答案,我想它可能会帮助别人!
我之前也遇到过同样的问题。对我来说,这是由于顺序问题。所以请确保先初始化/声明依赖对象,然后示例化/声明依赖对象。

uujelgoq

uujelgoq3#

pubspec.yaml中使用最新的get_it版本(现在是get_it: ^4.0.2)解决了这个问题。

llew8vvj

llew8vvj4#

在我的例子中,GetIt在我的import语句中遇到了困难。为了让它正确工作,你需要使用包导入而不是相对导入。
这样做:

import 'package:mypackage/features/some_file.dart';

字符串
而不是:

import '../features/some_file.dart';

qlfbtfca

qlfbtfca5#

我也面临过这个问题。没有什么是有意义的。然后我想起来了,我最近做了区分大小写的重命名。
我将Services/Database/Database.dart改为services/database/database.dart,但在一个文件中,我使用了小写版本的import,而在另一个文件中,它仍然是升级版本。

相关问题