gpt4 book ai didi

Flutter 使用 ProxyProvider

转载 作者:行者123 更新时间:2023-12-05 06:48:01 26 4
gpt4 key购买 nike

描述

我有一个页面 (InitialElementResultScreen) 必须等待在 InitialElementResultNotifier 中完成几个操作才能显示页面的其余部分。

有效的代码

我的 InitialElementResultScreen:

class InitialElementResultScreen extends StatelessWidget {
final String title;
InitialElementResultScreen({
Key key,
@required this.title,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBarWidget.withoutMenu(title: title),
body: _buildBody(context),
);
}

Widget _buildBody(BuildContext context){

// => For use variable in my screen I use the notifier.
var _initialElementResultProvider = Provider.of<InitialElementResultNotifier>(context);

return SingleChildScrollView(
child: Column(
children: [
ContainerComponent(
colorContainer: AppColors.backgroundLightBasic,
children: [
FutureBuilder(
future: _initialElementResultProvider.myFuture,
builder: (context, snapshot){
if(!snapshot.hasData) {
return Text('loading');
}else{
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// ... some widgets
],
);
}
}
)
],
),
],
),
);
}
}

当我调用我的 InitialElementResultScreen 时:

ChangeNotifierProvider<InitialElementResultNotifier>(
create: (BuildContext context) => InitialElementResultNotifier(getData: data)
)

我的 InitialElementResultNotifier 在其构造函数中直接接收外部数据。它将它们转换为对象,以便我可以对它们进行操作。

我的 InitialElementResultNotifier :

class InitialElementResultNotifier with ChangeNotifier{

// External Notifier
// ---------------------------------------------------------------------------

// Variables
// ---------------------------------------------------------------------------
FormInitialElementModel dataReceived;
Future myFuture;

// Constructor
// ---------------------------------------------------------------------------
InitialElementResultNotifier({
@required String getData,
}){
_initialise(getData);
}

// Initialisation
// ---------------------------------------------------------------------------
Future _initialise(String getData) async{
print('--- initialise');

dataReceived = await _convertDataReceived(getData);
myFuture = loadingInitialElementData();
}

// Functions public
// ---------------------------------------------------------------------------
Future<void> loadingInitialElementData() async
{
print('---------------------- dataReceived');
print(dataReceived); // => I see my data in the console
// ... some operations
}
}

无效的代码

在我的 loadingInitialElementData () 函数中,我想访问另一个通知程序。通知程序 LoaderNotifier 根据其状态管理不同消息的显示,我将根据我在 loadingInitialElementData() 函数中的操作更改消息。

为此,我将使用代理提供商。所以现在我这样调用我的 InitialElementResultScreen:

ChangeNotifierProvider<LoaderNotifier>(
create: (BuildContext context) => LoaderNotifier()
),
ProxyProvider<LoaderNotifier, InitialElementResultNotifier>(
update: (BuildContext context, LoaderNotifier loaderNotifier, InitialElementResultNotifier initialElementResultNotifier) {
return InitialElementResultNotifier(
getData: data,
loaderNotifier: loaderNotifier
);
}
),

我将更新我的InitialElementResultNotifier:

除了能够调用我的另一个通知程序的 3 行之外,所有代码都是相同的。我在代码中将这些行标记为注释

class InitialElementResultNotifier with ChangeNotifier{

// External Notifier
// ---------------------------------------------------------------------------
LoaderNotifier _loaderNotifier; // => New line !

// Variables
// ---------------------------------------------------------------------------
FormInitialElementModel dataReceived;
Future myFuture;

// Constructor
// ---------------------------------------------------------------------------
InitialElementResultNotifier({
@required String getData,
LoaderNotifier loaderNotifier, // => New line !
}){
_loaderNotifier = loaderNotifier; // => New line !
_initialise(getData);
}

// Initialisation
// ---------------------------------------------------------------------------
Future _initialise(String getData) async{
print('--- initialise');

dataReceived = await _convertDataReceived(getData);
myFuture = loadingInitialElementData();
}

// Functions public
// ---------------------------------------------------------------------------
Future<void> loadingInitialElementData() async
{
print('---------------------- dataReceived');
print(dataReceived); // => I see my data in the console
// ... some operations
}
}

错误

我不明白为什么会出现此错误,我的错误是什么?

======== Exception caught by widgets library =======================================================
The following assertion was thrown building InitialElementResultScreen(dirty, dependencies: [_InheritedProviderScope<InitialElementResultNotifier>]):
Tried to use Provider with a subtype of Listenable/Stream (InitialElementResultNotifier).


This is likely a mistake, as Provider will not automatically update dependents
when InitialElementResultNotifier is updated. Instead, consider changing Provider for more specific
implementation that handles the update mechanism, such as:

- ListenableProvider
- ChangeNotifierProvider
- ValueListenableProvider
- StreamProvider

Alternatively, if you are making your own provider, consider using InheritedProvider.

If you think that this is not an error, you can disable this check by setting
Provider.debugCheckInvalidValueType to `null` in your main file:

最佳答案

我很想帮助你,但我觉得我很难跟上。然而,我注意到的一件事可能会有所帮助,即在 InitialElementResultNotifier 中声明了 myFuture,但未初始化,并且在该类的任何位置都没有调用 notifyListeners 因此在 InitialElementResultScreenbuild 不会在 dataReceivedmyFuture 最终接收到值或更改时被调用。但是将 FutureBuilderProvider 结合使用的整个想法对我来说是非常错误的。如果您要继续进行此特定设计,则需要查看 Completers但我建议你不要这样做。

关于Flutter 使用 ProxyProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66938966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com