0

我正在尝试使用 library Provider 的 multiprovider。但它不起作用,因为尽管材料之前的上部小部件是 MultiProvider,但它无法找到正确的提供者。

我已经尝试导入任何东西,但它仍然无法正常工作

class _LoginViewState extends State<LoginView> {
  final TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return BaseView<LoginModel>(
      builder: (context, model, child) => Scaffold(
        backgroundColor: backgroundColor,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            StreamBuilder<Chat>(
              stream: Provider.of<Socketio>(context).streamChat,
              builder: (context, snapshot) {
                return Text(snapshot.data?.toString() ?? 'Foo');
              },
            ),
            LoginHeader(
              validationMessage: model.errorMessage,
              controller: _controller),
            model.state == ViewState.Busy
            ? CircularProgressIndicator()

那是我可以调用 Provider.of 的地方

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return  MultiProvider(
      providers: [
        StreamProvider<User>(
          initialData: User.initial(),
          builder: (context) => locator<AuthenticationService>().userController,
        ),
        StreamProvider<Chat>(
          initialData: Chat.initial(),
          builder: (context) => locator<Socketio>().chatController,
        ),
      ],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(),
        initialRoute: '/login',
        onGenerateRoute: Router.generateRoute,
      ),
    );
  }
}```
this is where I provider socket io provider before Maetrialapp inside Multi Provider
4

1 回答 1

1

问题是你正在做:

Provider.of<Socketio>(context)

但是您永远不会创建类型的提供者Provider<Socketio>

查看您的代码,您可能希望使用:

Provider.of<Chat>(context)
于 2019-05-20T04:57:00.433 回答