Reputation: 27
I am developing a Flutter app in Dart with Riverpod and have been working with Google Bard to code!
(I've learned more in the past two weeks with Bard than in the past year by myself, so highly recommend it!)
However, 'We' have written a State and Notifier class pair that should be working seamlessly together. But, the Notifier class is not seeing the properties that clearly are in the State class. Bard insists that this is impossible.
Here are the two classes:
class ImageUploadState {
const ImageUploadState._(this.value);
final String value;
static const initial = ImageUploadState._('');
static const uploading = ImageUploadState._('Uploading image...');
static ImageUploadState success(String message) =>
ImageUploadState._(message);
static ImageUploadState error(String message) => ImageUploadState._(message);
}
class ImageUploadNotifier extends StateNotifier<ImageUploadState> {
ImageUploadNotifier() : super(const ImageUploadState.initial());
Future<void> uploadImage(File imageFile) async {
state = const ImageUploadState.uploading();
try {
// Perform image upload logic using FirebaseStorage
// ...
state = ImageUploadState.success('Image uploaded successfully');
} catch (error) {
state = ImageUploadState.error(error.toString());
}
}
}
I have confirmed all IDE and dependency versions are latest stable.
Confirmed that all necessary imports are in place.
Confirmed that there are no conflicting imports.
I have invalidated caches and restarted.
I have confirmed that Dart code analysis enabled.
Changed class order.
Rebooted IDE, then computer...
Any idea why the State class properties are not recognized in the Notifier class?
Upvotes: 0
Views: 71
Reputation: 8529
If you want to fix the redirecting constructors, change this code:
static const initial = ImageUploadState._('');
static const uploading = ImageUploadState._('Uploading image...');
To this:
const ImageUploadState.initial() : this._('');
const ImageUploadState.uploading() : this._('Uploading image...');
Additionally, you might want to migrate from StateNotifier
to Notifier
, as StateNotifier
is now discouraged. You can see the migration guide here: https://riverpod.dev/docs/migration/from_state_notifier
Riverpod is rapidly changing, and even many tutorials on YouTube and articles out there are outdated, so make sure to always check the Riverpod documentation for the latest guide on using Riverpod.
Upvotes: 1