Finished user data retrieve into a state

This commit is contained in:
2024-01-11 15:41:06 +03:00
parent d10b1acf7f
commit 3015e91dd7
3 changed files with 14 additions and 17 deletions

View File

@@ -27,12 +27,7 @@ class MyProfileBloc extends Bloc<MyProfileEvent, MyProfileState> {
'Authorization': 'Bearer $token',
},
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
print(data);
} else {
// Обработка ошибки
}
final data = jsonDecode(response.body);
emit(state.copyWith(user: data));
}
}

View File

@@ -4,17 +4,19 @@ part of 'my_profile_bloc.dart';
/// Represents the state of MyProfile in the application.
class MyProfileState extends Equatable {
MyProfileState({this.k0ModelObj});
MyProfileState({this.myProfileModelObj, this.user});
MyProfileModel? k0ModelObj;
MyProfileModel? myProfileModelObj;
Map<String, dynamic>? user;
@override
List<Object?> get props => [
k0ModelObj,
myProfileModelObj,
];
MyProfileState copyWith({MyProfileModel? k0ModelObj}) {
MyProfileState copyWith(
{MyProfileModel? myProfileModelObj, Map<String, dynamic>? user}) {
return MyProfileState(
k0ModelObj: k0ModelObj ?? this.k0ModelObj,
);
myProfileModelObj: myProfileModelObj ?? this.myProfileModelObj,
user: user ?? this.user);
}
}

View File

@@ -17,7 +17,7 @@ class MyProfileScreen extends StatelessWidget {
static Widget builder(BuildContext context) {
return BlocProvider<MyProfileBloc>(
create: (context) =>
MyProfileBloc(MyProfileState(k0ModelObj: MyProfileModel()))
MyProfileBloc(MyProfileState(myProfileModelObj: MyProfileModel()))
..add(MyProfileInitialEvent()),
child: MyProfileScreen());
}
@@ -28,7 +28,7 @@ class MyProfileScreen extends StatelessWidget {
builder: (context, state) {
return SafeArea(
child: Scaffold(
appBar: _buildAppBar(context),
appBar: _buildAppBar(context, state.user!['name']),
body: Container(
width: double.maxFinite,
padding:
@@ -56,7 +56,7 @@ class MyProfileScreen extends StatelessWidget {
}
/// Section Widget
PreferredSizeWidget _buildAppBar(BuildContext context) {
PreferredSizeWidget _buildAppBar(BuildContext context, String username) {
return CustomAppBar(
height: 54.v,
leadingWidth: 28.h,
@@ -71,7 +71,7 @@ class MyProfileScreen extends StatelessWidget {
child: Row(children: [
AppbarTitleCircleimage(imagePath: ImageConstant.imgEllipse1),
AppbarSubtitle(
text: "Dmitry_Koval".tr,
text: username,
margin: EdgeInsets.only(left: 35.h, top: 12.v, bottom: 9.v))
])),
actions: [