删除streamBuilder加载小部件

时间:2019-10-04 18:17:51

标签: flutter dart

我从缓存中获取数据。单击搜索按钮时,显示RefreshProgressIndicator,然后显示material_search页面。是否可以删除if(snapshot.hasdata){}else{loading widget}上的加载而不会出现null错误消息? 如果不可能,那么如何在“ MaterialSerach”页面中使用“ StreamBuilder”?我使用了MaterialSearch插件。

builder: (BuildContext context) {
  return Material(
    child: StreamBuilder(
        stream: _subCategoryBloc.subCategory,
        builder: (context, AsyncSnapshot<List<SubCategorie>> snapshot) {
          if (snapshot.data != null) {
            return MaterialSearch<String>(
              barBackgroundColor: Color(0xFFEDB111),
              placeholder: 'Search',
              results: snapshot.data
                  .map((value) => MaterialSearchResult<String>(
                      value: value.subCategoryName,
                      subText: "${value.noOfMembers} Vendors",
                      categoryId: value.categoryId,
                      categoryName: value.categoryName,
                      subId: value.id,
                      text: value.subCategoryName))
                  .toList(),
              filter: (dynamic value, String criteria) {
                return value.toLowerCase().trim().contains(
                    RegExp(r'' + criteria.toLowerCase().trim() + ''));
              },
              onSubmit: (String value) {
                Navigator.of(context).pop(value);
              },
            );
          } else {
            return Center(
                child: RefreshProgressIndicator(
                    backgroundColor: Color(0xFFFFFFFF)));
          }
        }),
  );
});

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

builder: (BuildContext context) {
  return Material(
    child: StreamBuilder(
        stream: _subCategoryBloc.subCategory,
builder: (BuildContext context, processingSnapshot) {
        switch (processingSnapshot.connectionState) {
          case ConnectionState.active:
          case ConnectionState.done:
            if (processingSnapshot.hasData) {
              return MaterialSearch<String>(
              barBackgroundColor: Color(0xFFEDB111),
              placeholder: 'Search',
              results: snapshot.data
                  .map((value) => MaterialSearchResult<String>(
                      value: value.subCategoryName,
                      subText: "${value.noOfMembers} Vendors",
                      categoryId: value.categoryId,
                      categoryName: value.categoryName,
                      subId: value.id,
                      text: value.subCategoryName))
                  .toList(),
              filter: (dynamic value, String criteria) {
                return value.toLowerCase().trim().contains(
                    RegExp(r'' + criteria.toLowerCase().trim() + ''));
              },
              onSubmit: (String value) {
                Navigator.of(context).pop(value);
              },
            );
            } else {
              return Center(
                child: RefreshProgressIndicator(
                    backgroundColor: Color(0xFFFFFFFF)));
            }
            break;
          default:
            return CircularProgressIndicator();
        }
      },
 }),
  );
});