简体   繁体   English

视图 flutter 中的声明多提供者问题

[英]Problem with declaration multiprovider in view flutter

When i start my app, i'ad an problem with my multiprovider in flutter:当我启动我的应用程序时,我在 flutter 中的 multiprovider 出现问题:

'children != null && children.isNotEmpty': is not true

Error:错误:

The following assertion was thrown building Application(dirty): 'package:nested/nested.dart': Failed assertion: line 72 pos 16: 'children.= null && children:isNotEmpty'.在构建应用程序(脏)时抛出了以下断言:'package:nested/nested.dart':断言失败:第 72 行 pos 16:'children.= null && children:isNotEmpty'。 is not true.不是真的。 The relevant error-causing widget was Application lib\main:dart,8 When the exception was thrown: this was the stack #2 new Nested package.nested/nested:dart:72相关的导致错误的小部件是 Application lib\main:dart,8 抛出异常时:这是堆栈 #2 new Nested package.nested/nested:dart:72

The code:编码:

class Application extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: providers,
      child: _application(context),
    );
  }

  Widget _application(BuildContext context) {
    return MaterialApp(
      title: 'Simple Rest API',
      initialRoute: '/',
      routes: {
        '/': (context) => TeamsView(),
      },
    );
  }
}

EDIT:编辑:

My DI:我的 DI:

List<SingleChildWidget> providers = [
  ...services,
  ...datas,
  ...repositories,
  ...usescases,
];

List<SingleChildWidget> services = [];

List<SingleChildWidget> datas = [];

List<SingleChildWidget> repositories = [];

List<SingleChildWidget> usescases = [];

class ViewModelBuilder {
  static final _instances = {TeamsViewModel: () => TeamsViewModel()};

  static ViewModel instanciate(Type type) {
    return _instances[type]();
  }
}

My View:我的观点:

class TeamsView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return View<TeamsViewModel>(
      args: [],
      builderView: (context, model, child) => _buildView(context, model),
    );
  }
}

Widget _buildView(BuildContext context, TeamsViewModel model) {
  return Scaffold(
    appBar: AppBar(
      title: Text("Test"),
    ),
    body: Center(
      child: Text("Premier lancement"),
    ),
  );
}

My view model:我的观点 model:

class TeamsViewModel extends ViewModel {
  BuildContext _context;
  void load(BuildContext context, List args) async {
    _context = context;
    setLifecycle(OnLoad());
    setLifecycle(OnLoaded());
  }
}

if u want others code, ask me.如果你想要其他代码,问我。

As per the documentation , providers in your list should be structured like so:根据文档,您列表中的提供者的结构应如下所示:

MultiProvider(
  providers: [
    Provider<Something>(create: (_) => Something()),
    Provider<SomethingElse>(create: (_) => SomethingElse()),
    Provider<AnotherThing>(create: (_) => AnotherThing()),
  ],
  child: someWidget,
)

In other words, you should supply the create methods.换句话说,您应该提供create方法。

You are also not giving a list of providers, rather a list of values.您也没有提供提供者列表,而是提供值列表。

The problem is your list of SingleChildWidget s is empty (as it is the result of destructing 4 empty lists).问题是您的SingleChildWidget列表是空的(因为它是破坏 4 个空列表的结果)。 You must provide at least one SingleChildWidget.您必须提供至少一个 SingleChildWidget。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM