Flutter FutureBuilder: type 'Response' is not a subtype of type 'String'

Viewed 259

I'm trying to use flutter's FutureBuilder with an http.get() as the future parameter like so:

child: FutureBuilder(
  future: http.get(
      Uri.http('192.168.31.144:8000', 'api/v1/login')),
  builder: (context, snapshot) {
    if (snapshot.connectionState ==
        ConnectionState.done) {
      if (snapshot.hasError) {
        return Text('error');
      }
      return Text(snapshot.data);
    } else {
      return SpinKitFadingCircle(color: Colors.white);
    }
  },
),

I get the following error: The following _TypeError was thrown building FutureBuilder<Response>(dirty, state: _FutureBuilderState<Response>#0caa6): type 'Response' is not a subtype of type 'String'

From what I understand is that the FutureBuilder needs type Future<Response> as its future but http.get() is returning type String?

1 Answers

you have yo await for your future to come

Future<dynamic> getData() async {
return await http.get(Uri.http('192.168.31.144:8000', 'api/v1/login');
}



 child: FutureBuilder(
      future: getData(),
      builder: (context, snapshot) {
        if (snapshot.connectionState ==
            ConnectionState.done) {
          if (snapshot.hasError) {
            return Text('error');
          }
          return Text(snapshot.data);
        } else {
          return SpinKitFadingCircle(color: Colors.white);
        }
      },
    ),
Related