3

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?

  • hey. it has been a while, have you found anything that seems to be working? – sadsash Aug 12 '20 at 11:34
  • 2
    @sadsash!! Yeap!! I'm using the event source package. https://pub.dev/packages/eventsource/install – Rafael Bandeira Rodrigues Aug 17 '20 at 19:34
  • Thanks. I have seen this package, but did not think it would work for not events, but regular objects. Did you change your server to SSE from stream+json? If not, can you please add an answer to your question with related dart code sample? – sadsash Aug 19 '20 at 08:45

0 Answers0