简体   繁体   中英

Problem with declaration multiprovider in view flutter

When i start my app, i'ad an problem with my multiprovider in flutter:

'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'. 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

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:

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:

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.

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). You must provide at least one SingleChildWidget.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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