简体   繁体   English

当我尝试清理 stream 时,在 null 上调用了方法“map”

[英]Method 'map' was called on null when I try to clean the stream

I have a stream which feeds a list of items typed as 'Client'.我有一个 stream 提供输入为“客户端”的项目列表。 I have too a search bar which the user writes some name and the list should filter the results as searched and when Click at other button named 'clear' it should reset the list to default.我也有一个搜索栏,用户写一些名字,列表应该过滤搜索结果,当点击其他名为“清除”的按钮时,它应该将列表重置为默认值。 So, I have wrote these methods:所以,我写了这些方法:

//called when tap search button (if it has a text typed)
  void setSearch(String search) {
    _search = search;
    _streamController.add(null);//the error throws here
    load(clean: true);
  }

//called when tap 'clear' button
  void clearSearch() {
    _search = '';
    _offset = 1;
    _clients.clear();
    _streamController.stream.drain();
    _streamController.add(null); //the error throws here
    load(clean: true);
  }

But when I tap search or clear it throws this error:但是当我点击搜索或清除时,它会引发此错误:

Receiver: null.接收器:null。 NoSuchMethodError: The method 'map' was called on null. NoSuchMethodError:在 null 上调用了方法“map”。 Tried calling: map(Closure: (Map<dynamic, dynamic>) => Client)尝试调用: map(Closure: (Map<dynamic, dynamic>) => Client)

How can I handle this?我该如何处理? there is another way to clean the stream?还有另一种清洁 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 的方法吗? If you need more code I can update the question如果您需要更多代码,我可以更新问题

UPDATE This is my build method更新这是我的构建方法

Scaffold(
      appBar: appBar(),
      body: Column(
        children: [
          Container(
            padding: EdgeInsets.all(5.0),
            width: MediaQuery.of(context).size.width,
            height: 60.0,
            child: Card(
              elevation: 3,
              child: Container(
                child: TextField(
                  style: TextStyle(
                    color: AppColorSecondary,
                  ),
                  cursorColor: Theme.of(context).primaryColor,
                  textInputAction: TextInputAction.search,
                  decoration: InputDecoration(
                    prefixIcon: Icon(Icons.search, color: AppColorSecondary,),
                    hintStyle: TextStyle(color: AppColorSecondary),
                    filled: true,
                    fillColor: AppColorPrimary,
                    border: OutlineInputBorder(),
                    focusedBorder: OutlineInputBorder(),
                    suffixIcon: GestureDetector(
                      onTap: (){
                        searchController.clear();
                        clientsController.loadMore();
                      },
                      child: FittedBox(
                        alignment: Alignment.center,
                        fit: BoxFit.fitHeight,
                        child: IconTheme(
                          data: IconThemeData(),
                          child: Icon(
                            Icons.clear,
                            color: Colors.black,
                          ),
                        ),
                      ),
                    ),
                  ),
                  controller: searchController,
                  onSubmitted: (value) {
                    clientsController.setSearch(searchController.text);
                  },
                ),
              ),
            ),
          ),
          Expanded(
            child: StreamBuilder(
              stream: clientsController.stream,
              builder: (ctx, snapshot) {
                if (snapshot.error != null) {
                  print(snapshot.error.toString());
                  return Center(
                    child: Text(snapshot.error.toString()),
                  );
                } else if (!snapshot.hasData) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                } else if (snapshot.data.length == 0) {
                  return Center(
                    child: Text('0clients'),
                  );
                } else {
                  return RefreshIndicator(
                    onRefresh: clientsController.refresh,
                    child: ListView.builder(
                      controller: scrollController,
                      itemCount: snapshot.data.length + 1,
                      itemBuilder: (ctx, i) {
                        if (i < snapshot.data.length) {
                          return ClientsWidget(snapshot.data[i]);
                        } else if (clientsController.hasMore) {
                          return Padding(
                            padding: EdgeInsets.symmetric(vertical: 32.0),
                            child: Center(child: CircularProgressIndicator()),
                          );
                        } else {
                          return SizedBox(
                            height: 30,
                          );
                        }
                      },
                    ),
                  );
                }
              },
            ),
          )
        ],
      ),
    );
  }

Try _streamController.add([]);试试_streamController.add([]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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