Skip to content

Instantly share code, notes, and snippets.

@D-sense
Last active February 5, 2019 04:59
Show Gist options
  • Save D-sense/ed3fe3e53a80dd1f0020768e285f91ff to your computer and use it in GitHub Desktop.
Save D-sense/ed3fe3e53a80dd1f0020768e285f91ff to your computer and use it in GitHub Desktop.
I am getting an error:
type 'Future<dynamic>' is not a subtype of type
'StreamTransformer<List<dynamic>, List<dynamic>>" in my BLOC.
This is the structure of my app:
A BLOC (namely, reports_bloc.com).
A Repository (namely, repository.dart).
A API Provider (namely, api_provider.dart).
In detail, The API Provider file fetches the data online. The Repository file makes ready the data from API Provider (actually, I am also using DB, the reason why I choose to have a Repository file). The BLOC is meant to get data from Repository and add it to Stream and Sink accordingly. I believe the issue is in the BLOC (I am probably getting things wrong here as regards to adding to Stream and "Sinking" data).
//reports_bloc.dart
``import 'package:rxdart/rxdart.dart';
import '../resources/repository.dart';
class ReportsBloc {
final _repository = Repository();
final _allReports = PublishSubject<List<dynamic>>();
Observable<List<dynamic>> allReports;
ReportsBloc(){
allReports = _allReports.stream.transform(fetchAllReports());
}
fetchAllReports() async{
var reports = await _repository.fetchAllReports();
_allReports.sink.add(reports);
}
dispose() {
_allReports.close();
}
}``
//repository.dart
``import 'dart:async';
import 'api_provider.dart';
class Repository {
ApiProvider apiProvider = ApiProvider();
Future<List<dynamic>> fetchAllReports(){
var response = apiProvider.getAllReports();
return response;
}
}``
//api_provider.dart
``Future<List<dynamic>> getAllReports() async {
List<dynamic> reports = [];
final response = await http.get("$_root$_getAllReports");
var result = json.decode(response.body);
for(var i = 0; i < result.length; i++){
reports.add(result[i]);
print(reports[i]['type']);
}
return reports;
}
The Expected result is to be able to add data from the Repository and sink it.
@restacksyj
Copy link

This is a possible solution that your fetchAllReports is a future call so you can wrap it in async await and then return response from that. The problem is you are not waiting for the response to come back from the future call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment