Flutter 无法将此ValueListenableBuilder小部件标记为需要生成

Flutter 无法将此ValueListenableBuilder小部件标记为需要生成,flutter,Flutter,在我的应用程序的简单部分中,我将该值定义为ValueNotifier: final ValueNotifier<List<MediaDropDownStructure>> _mediaFoldersList = ValueNotifier<List<MediaDropDownStructure>>([]); StreamBuilder<List<MediaModel>>( stream: _globalBloc.s

在我的应用程序的简单部分中,我将该值定义为
ValueNotifier

final ValueNotifier<List<MediaDropDownStructure>> _mediaFoldersList = ValueNotifier<List<MediaDropDownStructure>>([]);
StreamBuilder<List<MediaModel>>(
    stream: _globalBloc.storageMediaBloc.imagesMedia$,
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return const Center(
          child: CircularProgressIndicator( ),
        );
      }
      final List<MediaModel> _allImages = snapshot.data;
      _mediaFoldersList.value = _allImages.map( (image) => MediaDropDownStructure( image.folder, image.folder ) ).toList();

      final MediaModel _all = _allImages[0];
      return GridView.builder(

      ...
我在DropDownBotton中使用它,比如:

child: ValueListenableBuilder(
  valueListenable: _mediaFoldersList,
    builder: (context, List<MediaDropDownStructure> items,child)=>DropdownButtonHideUnderline(
      child: DropdownButton<MediaDropDownStructure>(
        value: _chooseFolderName.value,
        hint: Text("please choose",style: AppTheme.of(context).caption(),),
        items: items.map((MediaDropDownStructure menuItem) {
          return DropdownMenuItem<MediaDropDownStructure>(
            value: menuItem,
            child: Text(menuItem.folderPath),
          );
        }).toList(),
        onChanged: (_) {},
      ),
    ),
),
子项:ValueListenableBuilder(
valueListenable:\u mediaFoldersList,
生成器:(上下文、列表项、子项)=>DropdownButtonHideUnderline(
孩子:下拉按钮(
值:\选择FolderName.value,
提示:文本(“请选择”,样式:AppTheme.of(context.caption(),),
items:items.map((MediaDropDownStructure菜单项){
返回下拉菜单项(
值:menuItem,
子项:文本(menuItem.folderPath),
);
}).toList(),
一旦改变:({},
),
),
),
我得到了这个错误:

The following assertion was thrown while dispatching notifications for ValueNotifier<List<MediaDropDownStructure>>:
setState() or markNeedsBuild() called during build.

This ValueListenableBuilder<List<MediaDropDownStructure>> widget cannot be marked as needing to build because the framework is already in the process of building widgets.  A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: ValueListenableBuilder<List<MediaDropDownStructure>>
  dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2b62a]]
  state: _ValueListenableBuilderState<List<MediaDropDownStructure>>#6c134
The widget which was currently being built when the offending call was made was: StreamBuilder<List<MediaModel>>
  dirty
  state: _StreamBuilderBaseState<List<MediaModel>, AsyncSnapshot<List<MediaModel>>>#cbf2d
为ValueNotifier发送通知时引发了以下断言:
在生成过程中调用setState()或markNeedsBuild()。
无法将此ValueListenableBuilder小部件标记为需要构建,因为框架已在构建小部件的过程中。只有当一个小部件的祖先当前正在构建时,才可以将其标记为需要在构建阶段构建。此异常是允许的,因为框架在子部件之前构建父部件,这意味着将始终构建脏子部件。否则,框架可能不会在构建阶段访问此小部件。
调用setState()或markNeedsBuild()的小部件是:ValueListenableBuilder
依赖项:[[u InheritedTheme,[u LocalizationsScope-[GlobalKey#2b62a]]
状态:_ValueListenableBuilderState#6c134
当发出违规调用时,当前正在生成的小部件是:StreamBuilder
肮脏的
状态:_StreamBuilderBaseState#cbf2d

问题在于,在streambuilder构建其状态的同时,\u mediaFoldersList的值也发生了变化,因此它也将开始构建ValueListenableBuilder,并且它正在创建问题,因为两个构建器不能一起构建

要解决此问题,您可以在1微秒后更改_mediaFoldersList的值,以便streambuilder完成其构建方法,然后ValueListenableBuilder可以构建

方法如下

 changevaluenotifiervalue(_allImages) async {

    await Future.delayed(Duration(microseconds: 1));
  _mediaFoldersList.value = _allImages.map( (image) => MediaDropDownStructure( image.folder, image.folder ) ).toList();

  }
在更改其值时调用此方法

final List<MediaModel> _allImages = snapshot.data;
      //_mediaFoldersList.value = _allImages.map( (image) => MediaDropDownStructure( image.folder, image.folder ) ).toList();  //commented 
  changevaluenotifiervalue(_allImages);  // added
  final MediaModel _all = _allImages[0];
final List\u allImages=snapshot.data;
//_mediaFoldersList.value=\u allImages.map((image)=>MediaDropDownStructure(image.folder,image.folder)).toList()//评论
changevaluenotifiervalue(_allImages);//补充
最终媒体模型_all=_allImages[0];

您能否添加更多详细信息,如您在哪里填写mediaFoldersList以及如何使用它构建下拉列表。@VirenVVarasadiya我的帖子和代码更新了很多,问题解决了。我在《颤栗》中学到了新的技巧,关于这个问题还有其他最好的解决方案吗?欢迎!。我试着找,但没找到。