wahyu
wahyu

Reputation: 2405

How to do stream builder to get data from bloc in flutter

I am new in BLOC and I am trying to read respond from api.. but whenever I call stream builder... my widget always stops in wait... here is my code here is api provider file

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;
  }
}

and here is for stream builder

                                      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;
                                              });
                                            },
                                          ),

is there a way so that I can call print(snapshot.data) inside if (snapshot.hasData)

Upvotes: 0

Views: 641

Answers (1)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27137

You need to pass argument which required in method otherwise it will not successfully responce (200) and it will throw error.

Upvotes: 1

Related Questions