Flutter GetIt 插件 - GetIt 中未注册类型 xxx

Mar*_*ula 3 state dart flutter

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

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");
}
Run Code Online (Sandbox Code Playgroud)

在主文件中调用

void main() {
  setupLocator();
  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)

我有一些检查定位器也正确返回我的 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();
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我在 ViewModel 中为 SignUpView 执行完全相同的操作,则会收到以下错误

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'
Run Code Online (Sandbox Code Playgroud)

在 AuthGuard 的 ViewModel 中,我确实成功地检索了 auth 服务。我还注释掉了定位器代码(因为我认为它可能是异步调用或类似的东西),但同样的错误仍然存​​在。

我正在使用 get_it: ^4.0.1 但降级到 3.xx 时错误仍然存​​在

在此处输入图片说明

这里是 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;
}
Run Code Online (Sandbox Code Playgroud)

Loï*_*kam 5

当要注册的类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());
}
Run Code Online (Sandbox Code Playgroud)


小智 1

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

  • 我使用相同的 get_it 版本,但仍然面临同样的问题 (2认同)