Flutter Stream with Spring WebFlux

Viewed 855

I'm stating one study with Spring WebFlux and I'd like to integrate it with Flutter to build one entire Reactive App.

On every site I found about Flutter Stream it shows Flutter + Firebase Stream, but I don't have needs about Firebase to build a simple stream project.

On my Spring controller, it looks like this:

@Slf4j
@RestController
@AllArgsConstructor
public class PlaceController {
    private final PlaceService service;
    private final PlaceMapper mapper;

    @GetMapping(value = "/list", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux<Place> getPlaceList(HttpServletRequest httpServletRequest) {
        log.info(httpServletRequest.getRequestURI());
        return service.findAll().map(item -> mapper.placeEntity2DTO(item));
    }
}

The Result of /list is:

{
  "id": "5f01a9a92f54fe0dacf22961",
  "name": "Place 1",
  "rating": 4.75,
  "description": "Description Test 1",
  "thumbnail": "",
  "cover": ""
}{
  "id": "5f01a9ad2f54fe0dacf22962",
  "name": "Place 2",
  "rating": 4.25,
  "description": "description test 2",
  "thumbnail": "",
  "cover": ""
}

Yes!! I noticed that it's not an array, probably because it's a Stream and it pushing data.

On Flutter side, I Don't know how to connect with this outside stream. The far I got is:

class PlaceRepository {
  static const String _URL_BASE = "${Environment.SERVER_PATH}/place";
  final Client _client = Client();

  Future<Stream<Place>> getPlaceList() async {
    Uri url = Uri.parse("$_URL_BASE/list");
    Request request = Request("get", url);
    StreamedResponse streamedResponse = await _client.send(request);

    return streamedResponse.stream
        .toStringStream()
        .transform(json.decoder)
        .map((data) => Place.fromJson(data));
  }
}

But it not working because the JSON came as the request above. So I'm thing i doing it the wrong way. Anyone knows how to connect the Spring WebFlux Stream with the Flutter Stream?

0 Answers
Related