Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: flutter: LateInitializationError: Field '__storage@230190796' has not been initialized. #4341

Open
the-best-is-best opened this issue Jan 20, 2025 · 4 comments
Assignees
Labels
bug Something isn't working needs repro info The issue is missing a reproduction sample and/or steps question Further information is requested waiting for response Waiting for follow up

Comments

@the-best-is-best
Copy link

HydratedBloc.storage = await HydratedStorage.build(
storageDirectory: kIsWeb
? HydratedStorageDirectory.web
: HydratedStorageDirectory((await getTemporaryDirectory()).path),
);

and

....

ProfileModel? get profile => _profile;
String? get profileImage => _profileImage;

@OverRide
ProfileState? fromJson(Map<String, dynamic> json) {
try {
// Deserialize JSON to profile state
final profileData = json['profile'] != null
? ProfileModel.fromJson(json['profile'])
: null;

  final dateTimeData = json['dateTime'] as String?;

  return ProfileHydratedStateLoaded(
    profile: profileData,
    dateTime: dateTimeData,
  );
} catch (_) {
  return null; // Return null if deserialization fails
}

}

Future<List> getAddress(String? search) async {
final res = await _searchAddressUseCase.call(search);

List<AddressDetailModel> result = [];

res.when(
  (v) {
    result = v;
  },
  (e) {
    result = [];
  },
);
return result;

}

@OverRide
Map<String, dynamic>? toJson(ProfileState state) {
if (state is ProfileGetDataSuccessState) {
// Serialize the profile state into JSON
return {
'profile': profile?.toJson(),
'dateTime': dateTime,
};
}
return null;
}
}

but flutter: LateInitializationError: Field '__storage@230190796' has not been initialized.

@the-best-is-best the-best-is-best added the bug Something isn't working label Jan 20, 2025
@felangel
Copy link
Owner

felangel commented Jan 20, 2025

Hi @the-best-is-best 👋
Can you please share the version of hydrated_bloc you’re using, the full stack trace, and ideally a full minimal reproduction sample that I can run/debug locally? Thanks! 🙏

@felangel felangel added question Further information is requested waiting for response Waiting for follow up needs repro info The issue is missing a reproduction sample and/or steps labels Jan 20, 2025
@felangel felangel self-assigned this Jan 20, 2025
@the-best-is-best
Copy link
Author

Used last version 10.0 and bloc Version 9

@felangel
Copy link
Owner

felangel commented Jan 23, 2025

Used last version 10.0 and bloc Version 9

Thanks! Can you please share the full stack trace along with a link to a minimal reproduction sample? It'd be much easier to help fix this if I'm able to reproduce it locally, thanks!

@felangel
Copy link
Owner

The only way I can see this being an issue is if you're using the HydratedMixin and if you forgot to call hydrate in the constructor:

class BrightnessCubit extends Cubit<Brightness> with HydratedMixin {
  BrightnessCubit() : super(Brightness.light) {
    hydrate(); // <--- When using the `HydratedMixin` you must call `hydrate` yourself.
  }

  void toggleBrightness() {
    emit(state == Brightness.light ? Brightness.dark : Brightness.light);
  }

  @override
  Brightness fromJson(Map<String, dynamic> json) {
    return Brightness.values[json['brightness'] as int];
  }

  @override
  Map<String, dynamic> toJson(Brightness state) {
    return <String, int>{'brightness': state.index};
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs repro info The issue is missing a reproduction sample and/or steps question Further information is requested waiting for response Waiting for follow up
Projects
None yet
Development

No branches or pull requests

2 participants