Skip to content

Instantly share code, notes, and snippets.

@VB10
Created November 1, 2021 22:08
Show Gist options
  • Select an option

  • Save VB10/139462c379d0bb5a6e11afb78dd972d8 to your computer and use it in GitHub Desktop.

Select an option

Save VB10/139462c379d0bb5a6e11afb78dd972d8 to your computer and use it in GitHub Desktop.
class UserView2 extends StatelessWidget {
const UserView2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider<UserCubit2>(
create: (context) => UserCubit2(context, userService: FirebaseUserSerivice()),
child: BlocConsumer<UserCubit2, UserState2>(
listener: (context, state) {
if (state.response != null) {
// navigate
}
},
builder: (context, state) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
context.read<UserCubit2>().updateTitle('hello');
},
),
appBar: AppBar(title: Text(state.response ?? '')),
body: ListView.builder(
itemCount: state.items?.length ?? 0,
itemBuilder: (BuildContext context, int index) {
return Text(state.items?[index] ?? '');
},
),
);
},
),
);
}
}
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:cubit_samples/product/duration_utils.dart';
import 'package:cubit_samples/user/service/user_service.dart';
class UserCubit2 extends Cubit<UserState2> {
UserCubit2(this.context, {required this.userService}) : super(UserState2());
final BuildContext context;
final IUser userService;
Future<void> fetchUserInformation(DurationShareds durationShareds) async {
emit(state.copyWith(isLoading: true));
await Future.delayed(Duration(seconds: 1));
emit(state.copyWith(isLoading: false, items: ['a'], response: 'Basarili'));
}
void updateTitle(String value) {
emit(state.copyWith(response: value));
}
}
class UserState2 {
final bool isLoading;
final String? response;
final List<String>? items;
UserState2({this.items, this.isLoading = false, this.response});
UserState2 copyWith({
bool? isLoading,
String? response,
List<String>? items,
}) {
return UserState2(
isLoading: isLoading ?? this.isLoading,
response: response ?? this.response,
items: items ?? this.items,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment