Reputation: 3476
I'm new to Flutter and following various guides to learn and, actually, after trying doing something simple by myself I found a weird issue and I don't know how to get rid of it...
These are my dependencies:
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^0.12.4
json_annotation: ^3.1.1
dio: ^3.0.10
dev_dependencies:
flutter_test:
sdk: flutter
freezed: ^0.12.7
build_runner: ^1.11.1
json_serializable: ^3.5.1
I create a small nodejs endpoint /status
that returns a simple object: { message: 'success' }
I created a model for the Status
:
part 'status_data.g.dart';
@JsonSerializable()
class StatusData {
StatusData({
this.message,
});
String message;
factory StatusData.fromRawJson(String str) =>
StatusData.fromJson(json.decode(str));
factory StatusData.fromJson(Map<String, dynamic> json) => StatusData(
message: json['message'],
);
}
I created a Status_notifier
:
class StatusNotifier extends StateNotifier<StatusState> {
final StatusClient _statusClient;
StatusNotifier(this._statusClient) : super(StatusState());
Future<void> getStatus() async {
try {
state = StatusState.loading();
final status = await _statusClient.getStatus();
state = StatusState.loaded(status); // ERROR (see below)
} catch (e) {
state = StatusState.error(message: 'Error: $e');
}
}
}
ERROR: The argument type 'StatusData (where StatusData is defined in /my_long_path/lib/models/status_data.dart)' can't be assigned to the parameter type 'StatusData (where StatusData is defined in /my_long_path/lib/application/state/status_state.freezed.dart)'.
I created a Status_state
:
For some reason here the import
for my StatusData
model is "grey", not used... It looks like it is using StatusData
coming from some generated file...
@freezed
abstract class StatusState with _$StatusState {
const factory StatusState() = Initial;
const factory StatusState.loading() = Loading;
const factory StatusState.loaded(StatusData status) = StatusData;
const factory StatusState.error({String message}) = Error;
}
I created the Status_client
to fetch data:
class StatusClient {
Future<StatusData> getStatus() async {
Response response =
await dio.get('https://my-api.com/status');
if (response.statusCode == 200) {
StatusData status = StatusData.fromJson(response.data);
return status;
}
throw response;
}
}
And finally the Status_provider
:
final statusClientProvider = Provider<StatusClient>((ref) => StatusClient());
final statusNotifierProvider = StateNotifierProvider(
(ref) => StatusNotifier(ref.watch(statusClientProvider)),
);
I also executed the command flutter pub run build_runner serve --delete-conflicting-outputs
to generate the files.
Also in my UI, withing the Consumer
, when the state is loaded
I have an error:
loaded: (status) => HomePageLoaded(status: status),
The error is identical at the one wrote above...
Can someone explain what is wrong with my implementation so that I can get better on it? Thanks in advance!
Upvotes: 0
Views: 1452
Reputation: 599
change the name of the redirected Construcor
const factory StatusState.loaded(StatusData status) = StatusData;
to
const factory StatusState.loaded(StatusData status) = StatusLoaded; // or make it private _StatusData
and re-run build runner
Upvotes: 1