如何做流建设者从扑扑集团数据

时间:2020-05-03 03:29:12

标签: flutter stream bloc

我是BLOC的新手,我正尝试读取api的响应。但是,每当我调用流生成器时……我的小部件始终在wait中停止……这是我的代码 这是api提供程序文件

class Provider {
  final _url = '...';

  Future<List<LoginRespon>> login(a, b) async {
    List<LoginRespon> datalogin = [];
    try {
        bool trustSelfSigned = true;
        HttpClient httpClient = new HttpClient()
          ..badCertificateCallback =
              ((X509Certificate cert, String host, int port) =>
                  trustSelfSigned);
        IOClient http = new IOClient(httpClient);
        final response = await http.post(_url,
            headers: {
              HttpHeaders.contentTypeHeader: 'application/json',
            },
            body: json.encode({
              "aa": a,
              "bb": b,
            }));
        Map<String, dynamic> responseJson = json.decode(response.body);

        if (responseJson["status"] == "200") {

          datalogin.add(LoginRespon(
              status: responseJson['status'],
              data: Data(
                  name: responseJson['data']['name'],
                  position: responseJson['data']['pos'])));
          return datalogin;
        } else {
          print("ppp");
        }

    } on Exception {
      rethrow;
    }
    return datalogin;
  }
}

这里是流构建器

                                      isClick
                                        ? StreamBuilder(
                                            stream: bloc.login(),
                                            builder: (context, snapshot) {
                                              if (snapshot.hasData) {
                                                print(snapshot.data);
                                                return Text("success");
                                              } else if (snapshot.hasError) {
                                                return Text(
                                                    snapshot.error.toString());
                                              }
                                              return Text("wait..");
                                            },
                                          )
                                        : FlatButton(
                                            child: Text("Login"),
                                            onPressed: () {
                                              setState(() {
                                                isClick = true;
                                              });
                                            },
                                          ),

有没有办法让我可以在print(snapshot.data)内致电if (snapshot.hasData)

1 个答案:

答案 0 :(得分:1)

您需要传递方法中要求的参数,否则将无法成功响应(200),并且会引发错误。