如何使用嵌套数据模型 [List > Item > Sublist > SubItem] 向提供者提供数据
How to provide data with provider using nested data models [List > Item > Sublist > SubItem]
我开始使用 Provider 包进行状态管理,并以基本的方式使用它。
随着应用程序变得越来越复杂,我想扩展使用范围。
现在我想到了这个模型结构:List<Client>
有一个 List<Product>
(更深的有一个 List<Component>
)。
我有一个为客户端使用 ChangeNotifierProvider 的 MultiProvider,这意味着 List<Client>
由提供者管理,到目前为止还不错。
现在我想直接在提供商中使用 List<Product>
,或者稍后在 List<Product>
中使用 List<Component>
。我不想通过 List<Client>
...一直到组件。
这里我有一张图像 map of the structure 可以可视化。
这里是一些简化的代码:
// Just an example idea of..
Class Product with ChangeNotifier {
final String title;
}
Class Client with ChangeNotifier {
final String name;
final String List<Product>;
}
Class Clients with ChangeNotifier {
final List<Client> _items;
}
void main() {
// start the app
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (ctx) => Clients()),
// How to provide a List<Product> that actually in the model
// belongs to a Client in the List<Client>
],
child: MaterialApp(
body: ...
)
);
}
}
所以主要问题是如何提供一个 List<Product>
实际上在模型中
属于 List<Client>
?
中的客户
感谢您在评论中的帮助。
似乎有两种可能的解决方案。
- 不要使用嵌套结构。这可以通过将父级的 id 保留在 before nested 列表中并将其扔出父级列表来实现。然后给它一个自己的 ChangeNotifierProvider 并在需要时按父 id 过滤项目。
所以在我的示例中,List 和 List 处于同一级别,并且都有一个
ChangeNotifierProvider。 The product model holds the id of it's client
并且该列表有一个方法 getbyClientId(clientId),该方法会生成针对指定客户端的过滤产品列表。就是这样。
- 使用 ChangeNotifierProxyProvider 将请求委托到嵌套列表中。
我选择了第一种方式,因为在我的情况下它似乎是正确的方式。在与它一起工作了一段时间后,我不能说这是一个错误的决定,所以我可以自由选择它。
可能对于其他情况,第二种方法可能更好。我没有在 ChangeNotifierProxyProvider 上投入太多时间,所以请查看其他来源以获取更多详细信息。
我开始使用 Provider 包进行状态管理,并以基本的方式使用它。 随着应用程序变得越来越复杂,我想扩展使用范围。
现在我想到了这个模型结构:List<Client>
有一个 List<Product>
(更深的有一个 List<Component>
)。
我有一个为客户端使用 ChangeNotifierProvider 的 MultiProvider,这意味着 List<Client>
由提供者管理,到目前为止还不错。
现在我想直接在提供商中使用 List<Product>
,或者稍后在 List<Product>
中使用 List<Component>
。我不想通过 List<Client>
...一直到组件。
这里我有一张图像 map of the structure 可以可视化。
这里是一些简化的代码:
// Just an example idea of..
Class Product with ChangeNotifier {
final String title;
}
Class Client with ChangeNotifier {
final String name;
final String List<Product>;
}
Class Clients with ChangeNotifier {
final List<Client> _items;
}
void main() {
// start the app
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (ctx) => Clients()),
// How to provide a List<Product> that actually in the model
// belongs to a Client in the List<Client>
],
child: MaterialApp(
body: ...
)
);
}
}
所以主要问题是如何提供一个 List<Product>
实际上在模型中
属于 List<Client>
?
感谢您在评论中的帮助。
似乎有两种可能的解决方案。
- 不要使用嵌套结构。这可以通过将父级的 id 保留在 before nested 列表中并将其扔出父级列表来实现。然后给它一个自己的 ChangeNotifierProvider 并在需要时按父 id 过滤项目。
所以在我的示例中,List 和 List 处于同一级别,并且都有一个
ChangeNotifierProvider。 The product model holds the id of it's client
并且该列表有一个方法 getbyClientId(clientId),该方法会生成针对指定客户端的过滤产品列表。就是这样。
- 使用 ChangeNotifierProxyProvider 将请求委托到嵌套列表中。
我选择了第一种方式,因为在我的情况下它似乎是正确的方式。在与它一起工作了一段时间后,我不能说这是一个错误的决定,所以我可以自由选择它。
可能对于其他情况,第二种方法可能更好。我没有在 ChangeNotifierProxyProvider 上投入太多时间,所以请查看其他来源以获取更多详细信息。